-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add validator that leverages symfony/validation constraints.
- Loading branch information
1 parent
a5cf78c
commit f329b83
Showing
7 changed files
with
162 additions
and
29 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
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
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,46 @@ | ||
<?php | ||
|
||
namespace Signify\ComposableValidators\Validators; | ||
|
||
use Signify\ComposableValidators\Traits\ValidatesMultipleFieldsWithConfig; | ||
use SilverStripe\Forms\FormField; | ||
use SilverStripe\Core\Validation\ConstraintValidator; | ||
|
||
/** | ||
* A validator which Validates values based on symfony validation constraints. | ||
* | ||
* Configuration values for this validator is an array of constraints to validate each field value against. | ||
* For example: | ||
* $validator->addField('IpAddress', [new Symfony\Component\Validator\Constraints\Ip(), new Symfony\Component\Validator\Constraints\NotBlank()]); | ||
* | ||
* See https://symfony.com/doc/current/reference/constraints.html for a list of constraints. | ||
* | ||
* This validator is best used within an AjaxCompositeValidator in conjunction with | ||
* a SimpleFieldsValidator. | ||
*/ | ||
class ConstraintsValidator extends BaseValidator | ||
{ | ||
use ValidatesMultipleFieldsWithConfig; | ||
|
||
/** | ||
* Validates that the required blocks exist in the configured positions. | ||
* | ||
* @param array $data | ||
* @return bool | ||
*/ | ||
public function php($data) | ||
{ | ||
foreach ($this->getFields() as $fieldName => $constraint) { | ||
$value = isset($data[$fieldName]) ? $data[$fieldName] : null; | ||
$this->result->combineAnd(ConstraintValidator::validate($value, $constraint, $fieldName)); | ||
} | ||
|
||
return $this->result->isValid(); | ||
} | ||
|
||
protected function getValidationHintForField(FormField $field): ?array | ||
{ | ||
// @TODO decide if there's a nice way to implement this | ||
return null; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace Signify\ComposableValidators\Tests; | ||
|
||
use Signify\ComposableValidators\Validators\ConstraintsValidator; | ||
use SilverStripe\Dev\SapphireTest; | ||
use Symfony\Component\Validator\Constraints\Ip; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
||
class ConstraintsValidatorTest extends SapphireTest | ||
{ | ||
public function provideValidation(): array | ||
{ | ||
return [ | ||
[ | ||
'fields' => ['FieldOne' => 'someValue'], | ||
'constraints' => ['FieldOne' => [new Ip()]], | ||
'isValid' => false, | ||
], | ||
[ | ||
'fields' => ['FieldOne' => 'someValue'], | ||
'constraints' => ['FieldOne' => [new NotBlank()]], | ||
'isValid' => true, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideValidation | ||
*/ | ||
public function testValidation(array $fields, array $constraints, bool $isValid): void | ||
{ | ||
$form = TestFormGenerator::getForm($fields, new ConstraintsValidator($constraints)); | ||
$result = $form->validationResult(); | ||
$this->assertSame($isValid, $result->isValid()); | ||
$messages = $result->getMessages(); | ||
if ($isValid) { | ||
$this->assertEmpty($messages); | ||
} else { | ||
$this->assertNotEmpty($messages); | ||
foreach ($messages as $message) { | ||
$this->assertSame(array_key_first($fields), $message['fieldName']); | ||
// It's up to the constraint what the message says, so testing it here could mean I have to update the | ||
// test if symfony changes their mind about it. For my purposes it's fine to just check that a message exists | ||
$this->assertNotEmpty($message['message']); | ||
} | ||
} | ||
} | ||
} |
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