-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Add FieldsValidator to ensure fields get validated
- Loading branch information
1 parent
2a56cc3
commit 3bfe760
Showing
4 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms; | ||
|
||
/** | ||
* Validates the internal state of all fields in the form. | ||
*/ | ||
class FieldsValidator extends Validator | ||
{ | ||
public function php($data): bool | ||
{ | ||
$valid = true; | ||
$fields = $this->form->Fields(); | ||
|
||
foreach ($fields as $field) { | ||
$valid = ($field->validate($this) && $valid); | ||
} | ||
|
||
return $valid; | ||
} | ||
|
||
public function canBeCached(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms\Tests; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\EmailField; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\FieldsValidator; | ||
use SilverStripe\Forms\Form; | ||
|
||
class FieldsValidatorTest extends SapphireTest | ||
{ | ||
protected $usesDatabase = false; | ||
|
||
public function provideValidation() | ||
{ | ||
return [ | ||
'missing values arent invalid' => [ | ||
'values' => [], | ||
'isValid' => true, | ||
], | ||
'empty values arent invalid' => [ | ||
'values' => [ | ||
'EmailField1' => '', | ||
'EmailField2' => null, | ||
], | ||
'isValid' => true, | ||
], | ||
'any invalid is invalid' => [ | ||
'values' => [ | ||
'EmailField1' => '[email protected]', | ||
'EmailField2' => 'not email', | ||
], | ||
'isValid' => false, | ||
], | ||
'all invalid is invalid' => [ | ||
'values' => [ | ||
'EmailField1' => 'not email', | ||
'EmailField2' => 'not email', | ||
], | ||
'isValid' => false, | ||
], | ||
'all valid is valid' => [ | ||
'values' => [ | ||
'EmailField1' => '[email protected]', | ||
'EmailField2' => '[email protected]', | ||
], | ||
'isValid' => true, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideValidation | ||
*/ | ||
public function testValidation(array $values, bool $isValid) | ||
{ | ||
$fieldList = new FieldList([ | ||
$field1 = new EmailField('EmailField1'), | ||
$field2 = new EmailField('EmailField2'), | ||
]); | ||
if (array_key_exists('EmailField1', $values)) { | ||
$field1->setValue($values['EmailField1']); | ||
} | ||
if (array_key_exists('EmailField2', $values)) { | ||
$field2->setValue($values['EmailField2']); | ||
} | ||
$form = new Form(null, 'testForm', $fieldList, new FieldList([/* no actions */]), new FieldsValidator()); | ||
|
||
$result = $form->validationResult(); | ||
$this->assertSame($isValid, $result->isValid()); | ||
$messages = $result->getMessages(); | ||
if ($isValid) { | ||
$this->assertEmpty($messages); | ||
} else { | ||
$this->assertNotEmpty($messages); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters