Skip to content

Commit

Permalink
FIX for #4417: Ensuring ->removeValidation() is defined on instances …
Browse files Browse the repository at this point in the history
…of Validator. Setup new API for enabling/disabling validation. Documentation and better type handling.
  • Loading branch information
patricknelson committed Aug 27, 2015
1 parent 958f98b commit e58df1c
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This validation class handles all form and custom form validation through
* the use of Required fields.
*
* Relies on javascript for client-side validation, and marking fields after serverside validation.
* Relies on javascript for client-side validation, and marking fields after server-side validation.
*
* Acts as a visitor to individual form fields.
*
Expand All @@ -21,7 +21,12 @@ abstract class Validator extends Object {
/**
* @var array $errors
*/
protected $errors;
protected $errors = array();

/**
* @var bool
*/
protected $enabled = true;

/**
* @param Form $form
Expand All @@ -37,13 +42,12 @@ public function setForm($form) {
/**
* Returns any errors there may be.
*
* @return null|array
* @return array
*/
public function validate() {
$this->errors = null;

$this->php($this->form->getData());

// Reset errors array (if already set).
$this->errors = array();
if ($this->enabled) $this->php($this->form->getData());
return $this->errors;
}

Expand Down Expand Up @@ -81,8 +85,10 @@ public function getErrors() {
}

/**
* @param string $fieldName
* @param array $data
* Ensures the specified field is not empty.
*
* @param string $fieldName The name of the valid you wish to ensure is not empty.
* @param array $data An array of field values you want to check against.
*/
public function requireField($fieldName, $data) {
if(is_array($data[$fieldName]) && count($data[$fieldName])) {
Expand Down Expand Up @@ -116,9 +122,33 @@ public function fieldIsRequired($fieldName) {
}

/**
* Performs actual validation. Data from the form is passed into this method.
*
* @param array $data
*
* @return mixed
*/
abstract public function php($data);

/**
* Ensures your validator will not perform any validation at all, in case the current user doesn't have access to
* edit any fields (for example).
*
* @param bool $enabled
* @return static
*/
public function setEnabled($enabled) {
$this->enabled = (bool) $enabled;
return $this;
}

/**
* An alias to ->setEnabled(false) for backward compatibility.
*
* @return static
*/
public function removeValidation() {
$this->setEnabled(false);
return $this;
}
}

0 comments on commit e58df1c

Please sign in to comment.