@@ -65,9 +65,9 @@ The validator class is also simple, and only has one required method ``validate(
65
65
public function validate($value, Constraint $constraint)
66
66
{
67
67
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();
71
71
);
72
72
}
73
73
}
@@ -76,11 +76,17 @@ The validator class is also simple, and only has one required method ``validate(
76
76
.. note ::
77
77
78
78
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.
84
90
85
91
Using the new Validator
86
92
-----------------------
0 commit comments