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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 3.1.2
    • Fix Version/s: None
    • Component/s: Forms Library
    • Labels:
      None
    • Affected Branches:
      MOODLE_31_STABLE

      Description

      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().)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              gormster Morgan Harris
              Participants:
              Component watchers:
              Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: