symfony/symfony-docs — Issue #1061: [Validator] Update documentation to reflect deprecated setMessage()
Author: bschussek
Created: 2012-02-05 10:48:22+00:00
State: open

The method `setMessage`, `getMessageTemplate` and `getMessageParameters` were removed from ConstraintValidator in 2.1. Instead you should call `addViolation` on the execution context now.

Before:

public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->setMessage($constraint->message, array(
'{{ value }}' => $value,
));

return false;
}
}

After:

public function isValid($value, Constraint $constraint)
{
// ...
if (!$valid) {
$this->context->addViolation($constraint->message, array(
'{{ value }}' => $value,
));

return false;
}
}

If your code (for instance a bundle) needs to preserve backwards compatibility with 2.0, you should also pass the validated `$value` as third parameter to `addViolation` (which then was required).