Skip to content

Commit 70c5ca1

Browse files
Rootieweaverryan
authored andcommitted
Update custom_contraint.rst to meet the new 2.5 api
1 parent 5dfe499 commit 70c5ca1

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

cookbook/validation/custom_constraint.rst

+14-8
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ The validator class is also simple, and only has one required method ``validate(
6565
public function validate($value, Constraint $constraint)
6666
{
6767
if (!preg_match('/^[a-zA-Za0-9]+$/', $value, $matches)) {
68-
$this->context->addViolation(
69-
$constraint->message,
70-
array('%string%' => $value)
68+
$this->context->buildViolation($constraint->message)
69+
->setParameter('%string%', $value)
70+
->addViolation();
7171
);
7272
}
7373
}
@@ -76,11 +76,17 @@ The validator class is also simple, and only has one required method ``validate(
7676
.. note::
7777

7878
The ``validate`` method does not return a value; instead, it adds violations
79-
to the validator's ``context`` property with an ``addViolation`` method
80-
call if there are validation failures. Therefore, a value could be considered
81-
as being valid if it causes no violations to be added to the context.
82-
The first parameter of the ``addViolation`` call is the error message to
83-
use for that violation.
79+
to the validator's ``context`` property. Therefore, a value could be considered
80+
as being valid if it causes no violations to be added to the context.The
81+
violation is constructed and added to the context using a
82+
``ConstraintViolationBuilder`` returned by the ``buildViolation`` method
83+
call. The parameter given to the ``buildViolation`` call is the error message
84+
to use for that violation. The ``addViolation`` method call finally adds the
85+
violation to the context.
86+
87+
.. versionadded:: 2.5
88+
The ``buildViolation`` method was added in Symfony 2.5. For usage examples with
89+
older Symfony versions, see the corresponding versions of this documentation page.
8490

8591
Using the new Validator
8692
-----------------------

0 commit comments

Comments
 (0)