diff --git a/src/Forms/RequiredFields.php b/src/Forms/RequiredFields.php index ec10ca2ff..9972f042d 100644 --- a/src/Forms/RequiredFields.php +++ b/src/Forms/RequiredFields.php @@ -48,6 +48,7 @@ class RequiredFields extends Validator */ public function removeValidation() { + parent::removeValidation(); $this->required = array(); return $this; diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index f14de905e..223eb229f 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -29,6 +29,11 @@ abstract class Validator extends Object */ protected $result; + /** + * @var bool + */ + private $enabled = true; + /** * @param Form $form * @return $this @@ -47,7 +52,9 @@ abstract class Validator extends Object public function validate() { $this->resetResult(); - $this->php($this->form->getData()); + if ($this->getEnabled()) { + $this->php($this->form->getData()); + } return $this->result; } @@ -129,6 +136,34 @@ abstract class Validator extends Object */ abstract public function php($data); + /** + * @param bool $enabled + * @return $this + */ + public function setEnabled($enabled) + { + $this->enabled = (bool)$enabled; + return $this; + } + + /** + * @return bool + */ + public function getEnabled() + { + return $this->enabled; + } + + /** + * @return $this + */ + public function removeValidation() + { + $this->setEnabled(false); + $this->resetResult(); + return $this; + } + /** * Clear current result * diff --git a/tests/php/Forms/ValidatorTest.php b/tests/php/Forms/ValidatorTest.php new file mode 100644 index 000000000..84d6e999e --- /dev/null +++ b/tests/php/Forms/ValidatorTest.php @@ -0,0 +1,58 @@ +add(new TextField($name)); + } + + return new Form(new Controller(), "testForm", $fieldList, new FieldList([/* no actions */])); + } + + + public function testRemoveValidation() + { + $validator = new TestValidator(); + + // Setup a form with the fields/data we're testing (a form is a dependency for validation right now). + $data = array("foobar" => ""); + $form = $this->getForm(array_keys($data)); // We only care right now about the fields we've got setup in this array. + $form->disableSecurityToken(); + $form->setValidator($validator); // Setup validator now that we've got our form. + $form->loadDataFrom($data); // Put data into the form so the validator can pull it back out again. + + $result = $form->validationResult(); + $this->assertFalse($result->isValid()); + $this->assertCount(1, $result->getMessages()); + + // Make sure it doesn't fail after removing validation AND has no errors (since calling validate should reset errors). + $validator->removeValidation(); + $result = $form->validationResult(); + $this->assertTrue($result->isValid()); + $this->assertEmpty($result->getMessages()); + } +} diff --git a/tests/php/Forms/ValidatorTest/TestValidator.php b/tests/php/Forms/ValidatorTest/TestValidator.php new file mode 100644 index 000000000..5cdc4c419 --- /dev/null +++ b/tests/php/Forms/ValidatorTest/TestValidator.php @@ -0,0 +1,23 @@ + $data) { + $this->validationError($field, 'error'); + } + } +}