Skip to content

2.5 Validation API changes #4233

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 10 commits into from
Oct 18, 2014
Prev Previous commit
[#4233][#4094] Making validateValue and validate changes
  • Loading branch information
weaverryan committed Oct 2, 2014
commit 9874d8e5d533cf40bd2e182fa53ee4271e641f18
21 changes: 17 additions & 4 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,13 @@ With this configuration, there are three validation groups:
fields only.

To tell the validator to use a specific group, pass one or more group names
as the second argument to the ``validate()`` method::
as the third argument to the ``validate()`` method::

$errors = $validator->validate($author, array('registration'));
// If you're using the new 2.5 validation API (you probably are!)
$errors = $validator->validate($author, null, array('registration'));

// If you're using the old 2.4 validation API
// $errors = $validator->validate($author, array('registration'));

If no groups are specified, all constraints that belong in group ``Default``
will be applied.
Expand Down Expand Up @@ -1189,10 +1193,19 @@ it looks like this::
$emailConstraint->message = 'Invalid email address';

// use the validator to validate the value
// If you're using the new 2.5 validation API (you probably are!)
$errorList = $this->get('validator')->validate(
$email,
$emailConstraint
);

// If you're using the old 2.4 validation API
/*
$errorList = $this->get('validator')->validateValue(
$email,
$emailConstraint
);
*/

if (count($errorList) == 0) {
// this IS a valid email address, do something
Expand All @@ -1206,13 +1219,13 @@ it looks like this::
// ...
}

By calling ``validateValue`` 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 .

The ``validateValue`` method returns a :class:`Symfony\\Component\\Validator\\ConstraintViolationList`
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,
which holds the error message on its ``getMessage`` method.
Expand Down