General developer forum

Use disabledif and required rule on a form element?

 
Easter Island / Rapa Nui May 2017
Use disabledif and required rule on a form element?
Group Plugin developers

Is it possible to use disabledif and required  on a single form element?

If an element is disabled then ignore the required rule.

If an element is enabled, then apply the required rule.

At the moment, if I uncheck the enabled checkbox, the elements are disabled but when I save I get an error saying the disabled element is required.

I'm using Moodle 3.0.10

$mform->addElement('advcheckbox', 'feature_enabled', get_string('enabled', 'local_myfeature'));
$mform->addElement('text', 'yourname', get_string('yourname', 'local_myfeature'));
$mform->disabledIf('yourname', 'feature_enabled');
$mform->addRule('yourname', get_string('required'), 'required');


 
Average of ratings: -
Picture of Darko Miletić
Re: Use disabledif and required rule on a form element?
Group Core developersGroup Particularly helpful Moodlers

It does not work that way so in short - no. To get that behavior you would have to tailor custom javascript code.

 
Average of ratings: Useful (1)
Easter Island / Rapa Nui May 2017
Re: Use disabledif and required rule on a form element?
Group Plugin developers

Okay, cheers Darko


 
Average of ratings: -