Make a Select Form Field Required
When adding a form rule for select (or multiple select) fields, these need to be applied on the client side (not server side).
Here’s an example of how to add a rule for a select field:
$mform->addRule( 'selectfield', get_string("selectfieldrequiredmessage", "local_yourplugin"), 'required', '', 'client' );
The key is the 5th parameter above which is set to
client instead of the default of server.
For more help, see the Moodle Forms Library Documentation