Skip to content

[Validator] Improve raw value validation #10782

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion validation/raw_values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ Validation of arrays is possible using the ``Collection`` constraint::

$validator = Validation::createValidator();

$input = array(
'name' => array(
'first_name' => 'Fabien',
'last_name' => 'Potencier',
),
'email' => 'test@email.tld',
'simple' => 'hello',
'gender' => 3,
'file' => null,
'password' => 'test',
'tags' => array(
array(
'slug' => 'symfony_doc',
'label' => 'symfony doc',
),
),
);

$groups = new Assert\GroupSequence(array('Default', 'custom'));

$constraint = new Assert\Collection(array(
// the keys correspond to the keys in the input array
'name' => new Assert\Collection(array(
Expand All @@ -62,9 +82,25 @@ Validation of arrays is possible using the ``Collection`` constraint::
'gender' => new Assert\Choice(array(3, 4)),
'file' => new Assert\File(),
'password' => new Assert\Length(array('min' => 60)),
'tags' => new Assert\Optional(array(
new Assert\Type('array'),
new Assert\Count(array('min' => 1)),
new Assert\All(array(
new Assert\Collection(array(
'slug' => array(
new Assert\NotBlank(),
new Assert\Type(array('type' => 'string'))
),
'label' => array(
new Assert\NotBlank(),
),
)),
new CustomUniqueTagValidator(array('groups' => 'custom')),
)),
)),
));

$violations = $validator->validate($input, $constraint);
$violations = $validator->validate($input, $constraint, $groups);

The ``validate()`` method returns a :class:`Symfony\\Component\\Validator\\ConstraintViolationList`
object, which acts just like an array of errors. Each error in the collection
Expand Down