8000 [Validator] Added array validation example to raw values by wouterj · Pull Request #7805 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Added array validation example to raw values #7805

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

Merged
merged 3 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions components/validator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ characters long::
}
}

The validator returns the list of violations.

Retrieving a Validator Instance
-------------------------------

Expand Down
24 changes: 23 additions & 1 deletion validation/raw_values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,34 @@ looks like this::
// ...
}

By calling ``validate`` on the validator, you can pass in a raw value and
By calling ``validate()`` on the validator, you can pass in a raw value and
the constraint object that you want to validate that value against. A full
list of the available constraints - as well as the full class name for each
constraint - is available in the :doc:`constraints reference </reference/constraints>`
section.

Validation of arrays is possible using the ``Collection`` constraint::

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;

$validator = Validation::createValidator();

$constraint = new Assert\Collection(array(
// the keys correspond to the keys in the input array
'name' => new Assert\Collection(array(
'first_name' => new Assert\Length(array('min' => 101)),
'last_name' => new Assert\Length(array('min' => 1)),
)),
'email' => new Assert\Email(),
'simple' => new Assert\Length(array('min' => 102)),
'gender' => new Assert\Choice(array(3, 4)),
'file' => new Assert\File(),
'password' => new Assert\Length(array('min' => 60)),
));

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

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