Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-83563

Unintuitive behavior of disabledIf and hideIf when dependenton is multi-select

XMLWordPrintable

      $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.

            bonczek Lars Bonczek
            bonczek Lars Bonczek
            Meirza Meirza
            Huong Nguyen Huong Nguyen
            CiBoT CiBoT
            Votes:
            4 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 7 hours, 19 minutes
                7h 19m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.