Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devrck/e23e7548989a2dbc0e1a9fd193edfbdd to your computer and use it in GitHub Desktop.
Save devrck/e23e7548989a2dbc0e1a9fd193edfbdd to your computer and use it in GitHub Desktop.

Revisions

  1. Bernhard Schussek revised this gist Jan 17, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions array-validation-error-mapping.php
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@
    )),
    'hobbies' => new Optional(array(
    new Type('array'),
    new All(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')),
    ))
    )),
    ))
    ))
    ),
    )),
    ));

  2. Bernhard Schussek created this gist Jan 17, 2014.
    73 changes: 73 additions & 0 deletions array-validation-error-mapping.php
    Original 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";
    20 changes: 20 additions & 0 deletions output
    Original 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.',
    ),
    ),
    ),
    )