Callback
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
The purpose of the Callback constraint is to create completely custom validation rules and to assign any validation errors to specific fields on your object. If you're using validation with forms, this means that you can make these custom errors display next to a specific field, instead of simply at the top of your form.
This process works by specifying one or more callback methods, each of which will be called during the validation process. Each of those methods can do anything, including creating and assigning validation errors.
Note
A callback method itself doesn't fail or return any value. Instead, as you'll see in the example, a callback method has the ability to directly add validator "violations".
Applies to | class |
Options | |
Class | Callback |
Validator | CallbackValidator |
Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;
class Author
{
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
// ...
}
}
The Callback Method
The callback method is passed a special ExecutionContextInterface
object.
You can set "violations" directly on this object and determine to which
field those errors should be attributed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// ...
use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;
class Author
{
// ...
private $firstName;
public function validate(ExecutionContextInterface $context)
{
// somehow you have an array of "fake names"
$fakeNames = array(/* ... */);
// check if the name is actually a fake name
if (in_array($this->getFirstName(), $fakeNames)) {
// If you're using the new 2.5 validation API (you probably are!)
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation();
// If you're using the old 2.4 validation API
/*
$context->addViolationAt(
'firstName',
'This name sounds totally fake!'
);
*/
}
}
}
Static Callbacks
You can also use the constraint with static methods. Since static methods don't have access to the object instance, they receive the object as the first argument:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
public static function validate($object, ExecutionContextInterface $context)
{
// somehow you have an array of "fake names"
$fakeNames = array(/* ... */);
// check if the name is actually a fake name
if (in_array($object->getFirstName(), $fakeNames)) {
// If you're using the new 2.5 validation API (you probably are!)
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation()
;
// If you're using the old 2.4 validation API
/*
$context->addViolationAt(
'firstName',
'This name sounds totally fake!'
);
*/
}
}
External Callbacks and Closures
If you want to execute a static callback method that is not located in the
class of the validated object, you can configure the constraint to invoke
an array callable as supported by PHP's call_user_func function.
Suppose your validation function is Acme\Validator::validate()
:
1 2 3 4 5 6 7 8 9 10 11 12 13
namespace Acme;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;
class Validator
{
public static function validate($object, ExecutionContextInterface $context)
{
// ...
}
}
You can then use the following configuration to invoke this validator:
1 2 3 4 5 6 7 8 9 10 11
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Callback({"Acme\Validator", "validate"})
*/
class Author
{
}
Note
The Callback constraint does not support global callback functions nor is it possible to specify a global function or a service method as callback. To validate using a service, you should create a custom validation constraint and add that new constraint to your class.
When configuring the constraint via PHP, you can also pass a closure to the constructor of the Callback constraint:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
// if you're using the older 2.4 validation API, you'll need this instead
// use Symfony\Component\Validator\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$callback = function ($object, ExecutionContextInterface $context) {
// ...
};
$metadata->addConstraint(new Assert\Callback($callback));
}
}
Options
callback
type: string
, array
or Closure
[default option]
The callback option accepts three different formats for specifying the callback method:
- A string containing the name of a concrete or static method;
- An array callable with the format
array('<Class>', '<method>')
; - A closure.
Concrete callbacks receive an ExecutionContextInterface instance as only argument.
Static or closure callbacks receive the validated object as the first argument and the ExecutionContextInterface instance as the second argument.
payload
type: mixed
default: null
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.
For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.