Skip to content

Commit 3dc14c1

Browse files
committed
Merge branch 'Rootie-patch-1' into validation-api-changes
* Rootie-patch-1: added a versionadded comment to Callback.rst Update custom_contraint.rst to meet the new 2.5 api Don't use deprecated functions in Callback.rst Conflicts: reference/constraints/Callback.rst
2 parents a1ffe74 + 9e4deaf commit 3dc14c1

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

cookbook/validation/custom_constraint.rst

Lines changed: 14 additions & 8 deletions
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
-----------------------

reference/constraints/Callback.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ those errors should be attributed::
116116
if (in_array($this->getFirstName(), $fakeNames)) {
117117
$context->buildViolation('This name sounds totally fake!')
118118
->atPath('firstName')
119-
->addViolation()
120-
;
119+
->addViolation();
121120
}
122121
}
123122
}
124123

124+
.. versionadded:: 2.5
125+
The ``buildViolation`` method was added in Symfony 2.5. For usage examples with
126+
older Symfony versions, see the corresponding versions of this documentation page.
127+
125128
Static Callbacks
126129
----------------
127130

0 commit comments

Comments
 (0)