-
Bug
-
Resolution: Fixed
-
Minor
-
4.4.5, 4.5.1, 5.0
$mform->disabledIf() and $mform->hideIf() do not behave intuitively when the element they are depending on is a select or autocomplete element with 'multiple' => true and the comparison value is empty.
Let's say I want to hide the checkbox "checkbox1" when any value is selected in the multiple select "multiselect1" (i.e., when it's not empty). I would try to write this as
$mform->hideIf('checkbox1', 'multiselect1[]', 'neq', []); |
(Note that we need to write "multiselect1[]" instead of "multiselect1" because that is the name of the HTML input.)
However, this does not work as expected. Moodle always hides the checkbox, even if no value is selected in the multi-select. If 'eq' or 'in' are used instead of 'neq', the checkbox is always visible instead.
How to reproduce
Code to create the form inputs (can be added to any form, for example in course/edit_form.php):
$mform->addElement('select', 'multiselect1', 'multiselect1', [1 => 'A', 2 => 'B'], ['multiple' => true]); |
$mform->addElement('checkbox', 'checkbox1', 'checkbox1'); |
What is expected
$mform->hideIf('checkbox1', 'multiselect1[]', 'neq', []); // Hidden if anything is selected. |
$mform->hideIf('checkbox1', 'multiselect1[]', 'eq', []); // Hidden if nothing is selected. |
$mform->hideIf('checkbox1', 'multiselect1[]', 'in', []); // Unclear (see below). Currently identical to 'eq'. |
What actually happens
$mform->hideIf('checkbox1', 'multiselect1[]', 'neq', []); // Always hidden. |
$mform->hideIf('checkbox1', 'multiselect1[]', 'eq', []); // Always visible. |
$mform->hideIf('checkbox1', 'multiselect1[]', 'in', []); // Always visible. |
Proposed solution
An empty list should be considered equal to another empty list, and not equal to any non-empty list. This would lead to the expected behavior.
Note: The 'in'-comparison currently behaves just like the 'eq'-comparison for multi-selects (i.e., the list of selected values must match the given list of values exactly). I would argue that it would make more sense to do the comparison separately for each selected element (i.e., each selected value must appear in the given list of values). This would mean that the 'in'-comparison succeeds if and only if the set of selected values is a subset of the given set of values. Otherwise, the 'in'-comparison doesn't serve any purpose in this situation.
- has been marked as being related by
-
MDL-85514 Forms hideIf/disabledIf 'in' rule not correctly implemented for multiselect/autocomplete
-
- Waiting for peer review
-
-
MDL-83947 PR only: MDL-83563 - Unintuitive behavior of disabledIf and hideIf when dependenton is multi-select
-
- Closed
-
-
MDL-84203 PR only (2) : MDL-83563 - Unintuitive behavior of disabledIf and hideIf when dependenton is multi-select
-
- Closed
-