Forked from webmozart/array-validation-error-mapping.php
Created
June 8, 2017 12:27
-
-
Save rn0/fe20992961b5280c0b5cb22337d5749f to your computer and use it in GitHub Desktop.
A little experiment: Validating (potentially multi-leveled) arrays with the Symfony2 Validator component and returning the errors in the same data structure as the validated array by using the Symfony2 PropertyAccess component.
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
<?php | |
use Symfony\Component\PropertyAccess\PropertyAccess; | |
use Symfony\Component\Validator\Constraints\All; | |
use Symfony\Component\Validator\Constraints\Choice; | |
use Symfony\Component\Validator\Constraints\Collection; | |
use Symfony\Component\Validator\Constraints\Length; | |
use Symfony\Component\Validator\Constraints\NotBlank; | |
use Symfony\Component\Validator\Constraints\Optional; | |
use Symfony\Component\Validator\Constraints\Required; | |
use Symfony\Component\Validator\Constraints\Type; | |
use Symfony\Component\Validator\ConstraintViolationInterface; | |
use Symfony\Component\Validator\Validation; | |
require_once __DIR__.'/vendor/autoload.php'; | |
$validator = Validation::createValidator(); | |
$propertyAccessor = PropertyAccess::createPropertyAccessor(); | |
$array = array( | |
'firstName' => 'Be', | |
'hobbies' => array( | |
array( | |
'name' => 'Hiking', | |
'frequency' => 'monthly', | |
), | |
array( | |
'name' => 'Cooking', | |
), | |
) | |
); | |
// Validate values | |
$constraint = new Collection(array( | |
'firstName' => new Required(array( | |
new NotBlank(), | |
new Length(array('min' => 3)), | |
)), | |
'lastName' => new Required(array( | |
new NotBlank(), | |
new Length(array('min' => 3)), | |
)), | |
'hobbies' => new Optional(array( | |
new Type('array'), | |
new All( | |
new Collection(array( | |
'name' => new Required(array( | |
new NotBlank(), | |
new Length(array('min' => 3)), | |
)), | |
'frequency' => new Required(array( | |
new Choice(array('daily', 'weekly', 'monthly')), | |
)) | |
)) | |
), | |
)), | |
)); | |
$violations = $validator->validateValue($array, $constraint); | |
// Use the same structure for the errors | |
$errors = array(); | |
foreach ($violations as $violation) { | |
/** @var ConstraintViolationInterface $violation */ | |
$entryErrors = (array) $propertyAccessor->getValue($errors, $violation->getPropertyPath()); | |
$entryErrors[] = $violation->getMessage(); | |
$propertyAccessor->setValue($errors, $violation->getPropertyPath(), $entryErrors); | |
} | |
var_export($errors); | |
echo "\n"; |
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
array ( | |
'firstName' => | |
array ( | |
0 => 'This value is too short. It should have 3 characters or more.', | |
), | |
'lastName' => | |
array ( | |
0 => 'This field is missing.', | |
), | |
'hobbies' => | |
array ( | |
1 => | |
array ( | |
'frequency' => | |
array ( | |
0 => 'This field is missing.', | |
), | |
), | |
), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment