Forked from webmozart/array-validation-error-mapping.php
Created
October 14, 2018 14:36
-
-
Save devrck/e23e7548989a2dbc0e1a9fd193edfbdd to your computer and use it in GitHub Desktop.
Revisions
-
Bernhard Schussek revised this gist
Jan 17, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -42,7 +42,7 @@ )), 'hobbies' => new Optional(array( new Type('array'), new All( new Collection(array( 'name' => new Required(array( new NotBlank(), @@ -51,8 +51,8 @@ 'frequency' => new Required(array( new Choice(array('daily', 'weekly', 'monthly')), )) )) ), )), )); -
Bernhard Schussek created this gist
Jan 17, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ <?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(array( 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ 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.', ), ), ), )