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 developer documentation.

 

Moodle Tips

 

Leave a Reply

Your email address will not be published. Required fields are marked *