Skip to content
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

Don't use deprecated functions in Callback.rst #4105

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cookbook/validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ 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)) {
$this->context->addViolation(
$constraint->message,
array('%string%' => $value)
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
);
}
}
Expand All @@ -76,11 +76,17 @@ The validator class is also simple, and only has one required method ``validate(
.. note::

The ``validate`` method does not return a value; instead, it adds violations
to the validator's ``context`` property with an ``addViolation`` method
call if there are validation failures. Therefore, a value could be considered
as being valid if it causes no violations to be added to the context.
The first parameter of the ``addViolation`` call is the error message to
use for that violation.
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.

.. 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.

Using the new Validator
-----------------------
Expand Down
22 changes: 10 additions & 12 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ those errors should be attributed::

// check if the name is actually a fake name
if (in_array($this->getFirstName(), $fakeNames)) {
$context->addViolationAt(
'firstName',
'This name sounds totally fake!',
array(),
null
);
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation();
}
}
}

.. 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.

Static Callbacks
----------------

Expand All @@ -137,12 +138,9 @@ 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)) {
$context->addViolationAt(
'firstName',
'This name sounds totally fake!',
array(),
null
);
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation();
}
}

Expand Down