-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add validator that leverages symfony/validation constraints. #6
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,52 @@ | ||
<?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,50 @@ | ||
<?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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No point using a dedicated validator for this anymore - if you REALLY need regex validation, there's a constraint for that. But there may well be a constraint (or combination of constraints) to validate whatever you were validating in a cleaner way than regex (e.g. if you were checking IP addresses, use the
Ip
constraint) so check that before resorting to the regex constraint itself.