Faulty door lock, but you knew that already.
The lock has several microswitches in it. The assembly that the microswitches are mounted on has been known to suffer from dry joints which make the car think the door isn't locked.
A dealer would replace the door lock. They can be disassembled and the soldering redone, but it isn't for the faint-hearted.