]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-18405 formslib: split is_validated into two separate functions for more flexibili...
authormjollnir_ <mjollnir_>
Tue, 2 Jun 2009 10:39:21 +0000 (10:39 +0000)
committermjollnir_ <mjollnir_>
Tue, 2 Jun 2009 10:39:21 +0000 (10:39 +0000)
lib/formslib.php

index ca850787d7d2432162b8f743b7be7d26b18aff6f..4b7fa99917d11d580501885758dc1753a7a2719c 100644 (file)
@@ -380,21 +380,43 @@ class moodleform {
 
     /**
      * Check that form data is valid.
+     * You should almost always use this, rather than {@see validate_defined_fields}
      *
      * @staticvar bool $validated
      * @return bool true if form data valid
      */
     function is_validated() {
-        static $validated = null; // one validation is enough
-        $mform =& $this->_form;
-
         //finalize the form definition before any processing
         if (!$this->_definition_finalized) {
             $this->_definition_finalized = true;
             $this->definition_after_data();
         }
 
-        if ($this->no_submit_button_pressed()){
+        return $this->validate_defined_fields();
+    }
+
+    /**
+     * Validate the form.
+     *
+     * You almost always want to call {@see is_validated} instead of this
+     * because it calls {@see definition_after_data} first, before validating the form,
+     * which is what you want in 99% of cases.
+     *
+     * This is provided as a separate function for those special cases where
+     * you want the form validated before definition_after_data is called
+     * for example, to selectively add new elements depending on a no_submit_button press,
+     * but only when the form is valid when the no_submit_button is pressed,
+     *
+     * @param boolean $validateonnosubmit optional, defaults to false.  The default behaviour
+     *                is NOT to validate the form when a no submit button has been pressed.
+     *                pass true here to override this behaviour
+     *
+     * @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) {
             $internal_val = $mform->validate();