Moodle
  1. Moodle
  2. MDL-26167

disabledIf doesn't work on filepicker form elements

    Details

    • Affected Branches:
      MOODLE_20_STABLE
    • Fixed Branches:
      MOODLE_20_STABLE
    • Rank:
      15889

      Description

      The nifty Javascript that $mform->disabledIf() uses to switch form elements to "disabled" doesn't work with a filepicker element. Sample code:

      // ... in a form definition
      $mform->addElement('select','enabler','Enable other form elements?', array(0=>'No',1=>'Yes'));

      // A text field will enable/disable properly
      $mform->addElement('text','foo','This is a text field');
      $mform->disabledIf('foo','enabler','neq',1);

      // A filepicker will remain enabled no matter what
      $filemanager_options = array();
      $filemanager_options['return_types'] = 3;
      $filemanager_options['accepted_types'] = 'web_image';
      $filemanager_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes);
      $mform->addElement('filepicker', 'bar', 'This is a filepicker', null, $filemanager_options);
      $mform->disabledIf('bar','enabler','neq',1);

        Issue Links

          Activity

          Hide
          Aaron Wells added a comment -

          The reason disabledIf() doesn't work with a filepicker is because the code looks for an HTML form element with a name equal to disabledIf()'s first argument, and adds/removes a "disabled=disabled" element on it.

          But, a filepicker renders into a "type=hidden" form element and some Javascript that generates the "Choose a file..." button. The problem is that it's the hidden element that has the matching name, while it's the Javascript-generated "Choose a file" button that we actually want to disable.

          Here's a patch that does a basic job of making this work. The outline is this:

          1. Add a flag to the hidden element to indicate it belongs to a filepicker

          2. Add a flag to the "Choose a file..." button to make it identifiable based on the name of the hidden element it corresponds to

          3. Change _disableElement() in lib/form/form.js so that it checks whether the element it's disabling is a filepicker, and if so, find the "Choose a file..." button corresponding to it and enable/disable that as well.

          I used the class attribute to flag the hidden element, and I used the name attribute to flag the "Choose a file..." button, which may not be the best way to go. But I think the general strategy outlined above is what we need to do.

          Show
          Aaron Wells added a comment - The reason disabledIf() doesn't work with a filepicker is because the code looks for an HTML form element with a name equal to disabledIf()'s first argument, and adds/removes a "disabled=disabled" element on it. But, a filepicker renders into a "type=hidden" form element and some Javascript that generates the "Choose a file..." button. The problem is that it's the hidden element that has the matching name, while it's the Javascript-generated "Choose a file" button that we actually want to disable. Here's a patch that does a basic job of making this work. The outline is this: 1. Add a flag to the hidden element to indicate it belongs to a filepicker 2. Add a flag to the "Choose a file..." button to make it identifiable based on the name of the hidden element it corresponds to 3. Change _disableElement() in lib/form/form.js so that it checks whether the element it's disabling is a filepicker, and if so, find the "Choose a file..." button corresponding to it and enable/disable that as well. I used the class attribute to flag the hidden element, and I used the name attribute to flag the "Choose a file..." button, which may not be the best way to go. But I think the general strategy outlined above is what we need to do.
          Hide
          Dan Marsden added a comment -

          adding Petr as a watcher to this - any chance you might be able to review this patch? - thanks!

          Show
          Dan Marsden added a comment - adding Petr as a watcher to this - any chance you might be able to review this patch? - thanks!
          Hide
          Jonathan Harker added a comment -

          +1 works okay here.

          Show
          Jonathan Harker added a comment - +1 works okay here.
          Hide
          Helen Foster added a comment -

          Aaron, thanks for your report and patch. Increasing priority and hoping this issue can be reviewed soon.

          Show
          Helen Foster added a comment - Aaron, thanks for your report and patch. Increasing priority and hoping this issue can be reviewed soon.
          Hide
          Dan Marsden added a comment -

          Pull request submitted for 2.0 Stable and master - thanks Aaron!

          Show
          Dan Marsden added a comment - Pull request submitted for 2.0 Stable and master - thanks Aaron!
          Hide
          Eloy Lafuente (stronk7) added a comment -

          Integrated for 20_STABLE and master. Will be available next weekly. Thanks a lot!

          Show
          Eloy Lafuente (stronk7) added a comment - Integrated for 20_STABLE and master. Will be available next weekly. Thanks a lot!

            People

            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: