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

Client-side validation of MoodleForms fails for multiple-element rules

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • None
    • 3.1.2
    • Forms Library
    • None
    • MOODLE_31_STABLE

      Rules in MoodleForms can take an array of element names as the "element". The PHP code responsible for generating the JavaScript code for client-side validation does not handle this properly, several times treating $element as if it were definitively an object when it might be an object or an array of objects.

      Steps to reproduce

      1. Create a PHP file called "formfail.php" and place it in the top level of your Moodle directory, with the following contents:

      <?php
       
      require_once('config.php');
      require_once('lib/formslib.php');
       
      require_login();
       
      $PAGE->set_context(context_system::instance());
      $PAGE->set_url(new moodle_url('/formfail.php'));
       
      class FailingForm extends moodleform {
          function definition() {
       
              $mform = $this->_form;
       
              $mform->addElement('text', 'small', 'Smaller number');
              $mform->setType('small', PARAM_INT);
       
              $mform->addElement('text', 'big', 'Bigger number');
              $mform->setType('big', PARAM_INT);
       
              $mform->addRule(['small', 'big'], 'Smaller must be smaller than bigger', 'compare', 'lt', 'client');
       
              $this->add_action_buttons();
       
          }
      }
       
      $form = new FailingForm();
       
      echo $OUTPUT->header();
      $form->display();
      echo $OUTPUT->footer();
      

      2. Navigate your browser to formfail.php

      Expected results

      The form should render properly with client-side validation

      Actual results

      Fatal error: Call to a member function getType() on a non-object in /Users/gormster/moodles/masterteameval/lib/formslib.php on line 2152

      Notes

      The offending lines are in MoodleQuickForm::getValidationScript. There are several instances throughout of $element being assumed to be a single element rather than an array of elements, even though there are also cases where it is clearly treated differently depending on whether it is an array or not.

      The specific line mentioned, along with a related bug in HTML_QuickForm_RuleRegistry::getValidationScript, also means that editor type elements cannot be used in multiple-element validation rules. (The bug there is that the $elementName parameter is ignored if $element is an array, and it just uses $element[$i]->getName().)

            Unassigned Unassigned
            gormster Morgan Harris
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

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