网页
综合
更新时间 2026-11-13 17:27:47
Callback (Symfony Docs)
自动蜘蛛
symfony.com
正文约 3405 字
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 instead of displaying custom errors at the top of the form, you can display them next to the field they apply to.
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.
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".
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:
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:
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():
You can then use the following configuration to invoke this validator:
The Callback constraint does not support global callback functions nor is it possible to specify a global function or a service method as a 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:
Using a Closure together with attribute configuration will disable the attribute cache for that class/property/method because Closure cannot be cached. For best performance, it's recommended to use a static callback method.
type: string, array or Closure
The callback option accepts three different formats for specifying the callback method:
Concrete callbacks receive an ExecutionContextInterface instance as the first argument and the payload option as the second argument.
Static or closure callbacks receive the validated object as the first argument, the ExecutionContextInterface instance as the second argument and the payload option as the third argument.
type: array | string default: null
It defines the validation group or groups of this constraint. Read more about validation groups.
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.
Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
Put the code quality back at the heart of your project
Thanks Linas Merkevicius for being a Symfony contributor
1 commit • 4 lines changed
View all contributors that help us make Symfony
Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.
Symfony™ is a trademark of Symfony SAS. All rights reserved.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。