Problem with multiple disabledIf - Moodle 2.2

Problem with multiple disabledIf - Moodle 2.2

by Tri Le -
Number of replies: 6

Multiple disableIf depending on the same element does not work well in Moodle 2.2 (it's fine in Moodle 2.1)

For example, assuming that I have a select box and a textbox

$mform->addElement('select', 'select1', get_string('...', '...'), array(1, 2, 3, 4));
$mform->addElement('text', 'text1', get_string('...', '...')_;

I'd like to disable the text when select = 1 or 2
$mform->disabledIf('text1', 'select1', 'eq', 1);
$mform->disabledIf('text1', 'select1', 'eq', 2);

In Moodle 2.2, just the first disabledIf command works. The second one is "hidden" by the first. Some debugging led me to the function convert_to_array in moodle_lib.php, the in_array condition eliminate the second rule so it doesn't appear in the json object passed to the javascript:

    foreach ($var as $key => $value) {
        // recursively convert objects
        if (is_object($value) || is_array($value)) {
            // but prevent cycles
            if (!in_array($value, $references)) {
                $result[$key] = convert_to_array($value);
                $references[] = $value;
            }
        } else {
            // simple values are untouched
            $result[$key] = $value;
        }
    }

Did someone encounter it or fix it yet!

Average of ratings: -
In reply to Tri Le

Re: Problem with multiple disabledIf - Moodle 2.2

by Luis de la Torre -

Same problem here.

It also happens on Moodle 2.3

By the way, http://docs.moodle.org/dev/lib/formslib.php_Form_Definition#disabledIf

states that:

$mform->disabledIf('mycontrol', 'someselect', 'eq', 42);

would disable my control UNLESS the dropdown has value 42. This is wrong. It disables my control WHEN the dropdown has value 42.

In reply to Luis de la Torre

Re: Problem with multiple disabledIf - Moodle 2.2

by Hubert Chathi -

Moodle docs is a wiki.  Please feel free to correct any errors you find.

In reply to Luis de la Torre

Re: Problem with multiple disabledIf - Moodle 2.2

by Tri Le -
Yes, you are right that the doc has a mistake, but the code does what it's supposed to do. If you want "unless", you should use neq, e.g. $mform->disabledIf('mycontrol', 'someselect', 'neq', 42); instead of 'eq'
In reply to Tri Le

Re: Problem with multiple disabledIf - Moodle 2.2

by Luis de la Torre -

Wow!!

I didn't know that, Tri. Thanks for telling. Im defenitely going to use it! smile

In reply to Luis de la Torre

Re: Problem with multiple disabledIf - Moodle 2.2

by Gabriel Mazetto -

I've also found problems while working with another piece of code using convert_to_array, and submited a bug report: 

http://tracker.moodle.org/browse/MDL-35001

We need to raise awerness of this problem, this will cause many other undesired side effects.