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
Next Next commit
Adding details about the 2.4 API as comments
  • Loading branch information
weaverryan committed Oct 2, 2014
commit 280440ea734e77cfe7ccfc1b100ad81940cd402c
30 changes: 18 additions & 12 deletions cookbook/validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,34 @@ The validator class is also simple, and only has one required method ``validate(
public function validate($value, Constraint $constraint)
{
if (!preg_match('/^[a-zA-Za0-9]+$/', $value, $matches)) {
// If you're using the new 2.5 validation API (you probably are!)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also have to add a comment about the 2.5 BC API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think so. If you're using 2.5 BC API, then the 2.5 validation API is available to you and you should be using it. I think the fact that you also have the ability to use the older API is a detail that they don't need to care about (that's just there so their old code doesn't break).

$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
);

// If you're using the old 2.4 validation API
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to either use either single line or multi line comments, not both:

// If you're using the old 2.4 validation API
// $this->context->addValidation(
//     ...
// );
/* If you're using the old 2.4 validation API
$this->context->addViolation(
    ...
);
*/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started to make this change, but I think it actually looked worse. The first line is a one-line comment, which you can keep even if you commented out the next block of code.

$this->context->addViolation(
$constraint->message,
array('%string%' => $value)
);
*/
}
}
}

.. note::

The ``validate`` method does not return a value; instead, it adds violations
to the validator's ``context`` property. Therefore, a value could be considered
as being valid if it causes no violations to be added to the context.The
violation is constructed and added to the context using a
``ConstraintViolationBuilder`` returned by the ``buildViolation`` method
call. The parameter given to the ``buildViolation`` call is the error message
to use for that violation. The ``addViolation`` method call finally adds the
violation to the context.
Inside ``validate``, you don't need to return a value. Instead, you add violations
to the validator's ``context`` property and a value will be considered valid
if it causes no violations. The ``buildViolation`` takes the error message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buildViolation method [...]

as its argument and returns an instance of
:class:`Symfony\\Component\\Validator\\Violation\\ConstraintViolationBuilder`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing dot at the end of the line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe we should use the interface here instead of the default implementation.

The ``addViolation`` method call finally adds the violation to the context.

.. versionadded:: 2.5
The ``buildViolation`` method was added in Symfony 2.5. For usage examples with
older Symfony versions, see the corresponding versions of this documentation page.
The ``buildViolation`` method was added in Symfony 2.5. For usage examples
with older Symfony versions, see the corresponding versions of this documentation
page.

Using the new Validator
-----------------------
Expand Down
27 changes: 25 additions & 2 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Configuration

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;

class Author
{
Expand Down Expand Up @@ -101,6 +103,8 @@ those errors should be attributed::

// ...
use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;

class Author
{
Expand All @@ -114,16 +118,26 @@ those errors should be attributed::

// check if the name is actually a fake name
if (in_array($this->getFirstName(), $fakeNames)) {
// If you're using the new 2.5 validation API (you probably are!)
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation();

// If you're using the old 2.4 validation API
/*
$context->addViolationAt(
'firstName',
'This name sounds totally fake!'
);
*/
}
}
}

.. versionadded:: 2.5
The ``buildViolation`` method was added in Symfony 2.5. For usage examples with
older Symfony versions, see the corresponding versions of this documentation page.
The ``buildViolation`` method was added in Symfony 2.5. For usage examples
with older Symfony versions, see the corresponding versions of this documentation
page.

Static Callbacks
----------------
Expand All @@ -138,10 +152,17 @@ have access to the object instance, they receive the object as the first argumen

// check if the name is actually a fake name
if (in_array($object->getFirstName(), $fakeNames)) {
// If you're using the new 2.5 validation API (you probably are!)
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation()
;

// If you're using the old 2.4 validation API
$context->addViolationAt(
'firstName',
'This name sounds totally fake!'
);
}
}

Expand All @@ -156,6 +177,8 @@ your validation function is ``Vendor\Package\Validator::validate()``::
namespace Vendor\Package;

use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;

class Validator
{
Expand Down