Merge branch 'MDL-56250-32-formvalidation' of https://github.com/roperto/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 25 Oct 2016 18:23:12 +0000 (20:23 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 25 Oct 2016 18:23:12 +0000 (20:23 +0200)
1  2 
lib/formslib.php
lib/tests/formslib_test.php

diff --combined lib/formslib.php
@@@ -139,6 -139,9 +139,9 @@@ abstract class moodleform 
      /** @var object definition_after_data executed flag */
      protected $_definition_finalized = false;
  
+     /** @var bool|null stores the validation result of this form or null if not yet validated */
+     protected $_validated = null;
      /**
       * The constructor function calls the abstract function definition() and it will then
       * process and clean and attempt to validate incoming data.
       * @return bool true if form data valid
       */
      function validate_defined_fields($validateonnosubmit=false) {
-         static $validated = null; // one validation is enough
          $mform =& $this->_form;
          if ($this->no_submit_button_pressed() && empty($validateonnosubmit)){
              return false;
-         } elseif ($validated === null) {
+         } elseif ($this->_validated === null) {
              $internal_val = $mform->validate();
  
              $files = array();
                  $moodle_val = true;
              }
  
-             $validated = ($internal_val and $moodle_val and $file_val);
+             $this->_validated = ($internal_val and $moodle_val and $file_val);
          }
-         return $validated;
+         return $this->_validated;
      }
  
      /**
@@@ -2750,6 -2752,8 +2752,6 @@@ class MoodleQuickForm_Renderer extends 
          $this->_collapseButtons = '';
          $formid = $form->getAttribute('id');
          parent::startForm($form);
 -        // HACK to prevent browsers from automatically inserting the user's password into the wrong fields.
 -        $this->_hiddenHtml .= prevent_form_autofill_password();
          if ($form->isFrozen()){
              $this->_formTemplate = "\n<div class=\"mform frozen\">\n{content}\n</div>";
          } else {
@@@ -555,10 -555,8 +555,10 @@@ class core_formslib_testcase extends ad
       * MDL-52873
       */
      public function test_multiple_modgrade_fields() {
 +        global $CFG;
          $this->resetAfterTest(true);
  
 +        $CFG->theme = 'clean';
          $form = new formslib_multiple_modgrade_form();
          ob_start();
          $form->display();
       * Test persistant freeze elements have different id's.
       */
      public function test_persistantrreeze_element() {
 +        global $CFG;
          $this->resetAfterTest(true);
 +        $CFG->theme = 'clean';
  
          $form = new formslib_persistantrreeze_element();
          ob_start();
          $this->assertNotTag(array('id' => 'id_textfrozen_persistant'), $html);
  
      }
+     /**
+      * Ensure a validation can run at least once per object. See MDL-56259.
+      */
+     public function test_multiple_validation() {
+         $this->resetAfterTest(true);
+         // It should be valid.
+         formslib_multiple_validation_form::mock_submit(['somenumber' => '10']);
+         $form = new formslib_multiple_validation_form();
+         $this->assertTrue($form->is_validated());
+         $this->assertEquals(10, $form->get_data()->somenumber);
+         // It should not validate.
+         formslib_multiple_validation_form::mock_submit(['somenumber' => '-5']);
+         $form = new formslib_multiple_validation_form();
+         $this->assertFalse($form->is_validated());
+         $this->assertNull($form->get_data());
+     }
  }
  
  
@@@ -933,3 -948,35 +952,35 @@@ class formslib_persistantrreeze_elemen
          $mform->setType('textnotpersistant', PARAM_TEXT);
      }
  }
+ /**
+  * Used to test that you can validate a form more than once. See MDL-56250.
+  * @package    core_form
+  * @author     Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
+  * @copyright  2016 Catalyst IT
+  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+  */
+ class formslib_multiple_validation_form extends moodleform {
+     /**
+      * Simple definition, one text field which can have a number.
+      */
+     public function definition() {
+         $mform = $this->_form;
+         $mform->addElement('text', 'somenumber');
+         $mform->setType('somenumber', PARAM_INT);
+     }
+     /**
+      * The number cannot be negative.
+      * @param array $data An array of form data
+      * @param array $files An array of form files
+      * @return array Error messages
+      */
+     public function validation($data, $files) {
+         $errors = parent::validation($data, $files);
+         if ($data['somenumber'] < 0) {
+             $errors['somenumber'] = 'The number cannot be negative.';
+         }
+         return $errors;
+     }
+ }