Permalink
Browse files

MDL-18405 formslib: split is_validated into two separate functions fo…

…r more flexibility for callers to override the default behaviour.
  • Loading branch information...
mjollnir_
mjollnir_ committed Jun 2, 2009
1 parent dba840d commit e2a1ed12de94b3605367ff4ec12d1fe5fa11be1c
Showing with 26 additions and 4 deletions.
  1. +26 −4 lib/formslib.php
View
@@ -300,20 +300,42 @@ function no_submit_button_pressed(){
/**
* Check that form data is valid.
+ * You should almost always use this, rather than {@see validate_defined_fields}
*
* @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();
}
+ 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()){
+ if ($this->no_submit_button_pressed() && empty($validateonnosubmit)){
return false;
} elseif ($validated === null) {
$internal_val = $mform->validate();

0 comments on commit e2a1ed1

Please sign in to comment.