mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #4542 from patricknelson/issue-4417-validator-remove-validation-master
FIX for #4417: Ensuring ->removeValidation() is defined on instances of Validator. Setup new API for enabling/disabling validation. Documentation and better type handling.
This commit is contained in:
commit
8ed675d29b
@ -48,6 +48,7 @@ class RequiredFields extends Validator
|
|||||||
*/
|
*/
|
||||||
public function removeValidation()
|
public function removeValidation()
|
||||||
{
|
{
|
||||||
|
parent::removeValidation();
|
||||||
$this->required = array();
|
$this->required = array();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -29,6 +29,11 @@ abstract class Validator extends Object
|
|||||||
*/
|
*/
|
||||||
protected $result;
|
protected $result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $enabled = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Form $form
|
* @param Form $form
|
||||||
* @return $this
|
* @return $this
|
||||||
@ -47,7 +52,9 @@ abstract class Validator extends Object
|
|||||||
public function validate()
|
public function validate()
|
||||||
{
|
{
|
||||||
$this->resetResult();
|
$this->resetResult();
|
||||||
$this->php($this->form->getData());
|
if ($this->getEnabled()) {
|
||||||
|
$this->php($this->form->getData());
|
||||||
|
}
|
||||||
return $this->result;
|
return $this->result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,6 +136,34 @@ abstract class Validator extends Object
|
|||||||
*/
|
*/
|
||||||
abstract public function php($data);
|
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
|
* Clear current result
|
||||||
*
|
*
|
||||||
|
58
tests/php/Forms/ValidatorTest.php
Normal file
58
tests/php/Forms/ValidatorTest.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Forms\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\Form;
|
||||||
|
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
|
||||||
|
use SilverStripe\Forms\TextField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package framework
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
class ValidatorTest extends SapphireTest
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Common method for setting up form, since that will always be a dependency for the validator.
|
||||||
|
*
|
||||||
|
* @param array $fieldNames
|
||||||
|
* @return Form
|
||||||
|
*/
|
||||||
|
protected function getForm(array $fieldNames = array())
|
||||||
|
{
|
||||||
|
// Setup field list now. We're only worried about names right now.
|
||||||
|
$fieldList = new FieldList();
|
||||||
|
foreach ($fieldNames as $name) {
|
||||||
|
$fieldList->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());
|
||||||
|
}
|
||||||
|
}
|
23
tests/php/Forms/ValidatorTest/TestValidator.php
Normal file
23
tests/php/Forms/ValidatorTest/TestValidator.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Forms\Tests\ValidatorTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\Forms\Validator;
|
||||||
|
|
||||||
|
class TestValidator extends Validator implements TestOnly
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requires a specific field for test purposes.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
|
public function php($data)
|
||||||
|
{
|
||||||
|
foreach ($data as $field => $data) {
|
||||||
|
$this->validationError($field, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user