If a flight being illegal meant that there was no commander, I suspect that CAA Enforcement branch would have to be largely made redundant.
Of course you can be pilot in command of an illegal flight. A stupid thing to do knowingly, but you'd still be in command.
G