Required select dropdown where first option 'Choose an option' must raise an alert

Required select dropdown where first option 'Choose an option' must raise an alert

by Franky Just -
Number of replies: 5

I'm using in a form a select / dropdown, which I want to be required. I want the first 'option' to be set to 'Choose an option'. Is it possible to add a rule, when the first 'option' is chosen, to show the alert that an option has to be chosen?

Average of ratings: -
In reply to Franky Just

Re: Required select dropdown where first option 'Choose an option' must raise an alert

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You can use addRule to add simple validation rules to your fields. In this case, you could set your "Choose an option" option to an empty value and set a rule for the field to have minlength 1, so it will give you an error if this option is selected.

In reply to Mark Johnson

Re: Required select dropdown where first option 'Choose an option' must raise an alert

by Franky Just -

Thx. There are more options I've found out.

1. With the addRule and then use Rule type minlenght which in my case has to be set to 2, because the value of the first option is in my case '0'. The other options are all text (letters) values longer than 2, so this is no problem. And in my case also Rule type lettersonly could be used. 

2. With a separate validation function on the field to generate an error when choosing the default first value (0). This option would be the safest because the value on which the error has to be generated, can exactly been set.

The option from Darko seems not been working in my case. The select values - except the first - are all letter strings, and using the Rule type nonzero does not raise an error.

In reply to Franky Just

Re: Required select dropdown where first option 'Choose an option' must raise an alert

by Darko Miletić -

You did not provide complete information with regards to type of options you expect to have. In case if we have mixed type of keys different type of rules needs to be used. For example:

        $mform->addElement('hidden', 'zerovalue', '0');
        $mform->setType('zerovalue', PARAM_ALPHANUM);

        $options = [
            '0' => get_string('none'),
            'aa' => 'aa',
            'bb' => 'bb',
            'cc' => 'cc',
        ];

        $mform->addElement('select', 'myselect', 'select', $options);
        $mform->setType('myselect', PARAM_ALPHANUM);
        $mform->addRule(['myselect', 'zerovalue'], 'message text', 'compare', 'neq', 'server');




In reply to Darko Miletić

Re: Required select dropdown where first option 'Choose an option' must raise an alert

by Franky Just -

Thx. This option works also, so there are may options to choose from. Maybe the mixed types of keys option could be added to the documentation, I didn't came across this option anywhere else.

In reply to Franky Just

Re: Required select dropdown where first option 'Choose an option' must raise an alert

by Darko Miletić -

Te elaborate further on what Mark said you would need something like this:

       $options = [
          0 => get_string('none'),
          1 => get_string('something'),
          2 => get_string('something1'),
          3 => get_string('something2'),
       ];

        $mform->addElement(
            'select', 'myselect', 'select', $options
        );
        $mform->setType('myselect', PARAM_INT);

        $mform->addRule(
            'myselect', 'message text', 'nonzero', null, 'client'
        );