@@ -2,10 +2,10 @@ Callback
22========
33
44The purpose of the Callback assertion is to let you create completely custom
5- validation rules and to assign any validation errors to specific fields on
6- your object. If you're using validation with forms, this means that you can
7- make these custom errors display next to a specific field, instead of simply
8- at the top of your form.
5+ validation rules and to assign any validation errors to specific fields
6+ on your object. If you're using validation with forms, this means that you
7+ can make these custom errors display next to a specific field, instead of
8+ simply at the top of your form.
99
1010This process works by specifying one or more *callback * methods, each of
1111which will be called during the validation process. Each of those methods
9292 The Callback Method
9393-------------------
9494
95- The callback method is passed a special ``ExecutionContextInterface `` object. You
96- can set "violations" directly on this object and determine to which field
97- those errors should be attributed::
95+ The callback method is passed a special ``ExecutionContextInterface `` object.
96+ You can set "violations" directly on this object and determine to which
97+ field those errors should be attributed::
9898
9999 // ...
100100 use Symfony\Component\Validator\ExecutionContextInterface;
@@ -111,7 +111,12 @@ those errors should be attributed::
111111
112112 // check if the name is actually a fake name
113113 if (in_array($this->getFirstName(), $fakeNames)) {
114- $context->addViolationAt('firstname', 'This name sounds totally fake!', array(), null);
114+ $context->addViolationAt(
115+ 'firstname',
116+ 'This name sounds totally fake!',
117+ array(),
118+ null
119+ );
115120 }
116121 }
117122 }
@@ -129,9 +134,10 @@ process. Each method can be one of the following formats:
129134
1301351) **String method name **
131136
132- If the name of a method is a simple string (e.g. ``isAuthorValid ``), that
133- method will be called on the same object that's being validated and the
134- ``ExecutionContextInterface `` will be the only argument (see the above example).
137+ If the name of a method is a simple string (e.g. ``isAuthorValid ``),
138+ that method will be called on the same object that's being validated
139+ and the ``ExecutionContextInterface `` will be the only argument (see
140+ the above example).
135141
1361422) **Static array callback **
137143
@@ -197,15 +203,19 @@ process. Each method can be one of the following formats:
197203 {
198204 $metadata->addConstraint(new Callback(array(
199205 'methods' => array(
200- array('Acme\BlogBundle\MyStaticValidatorClass', 'isAuthorValid'),
206+ array(
207+ 'Acme\BlogBundle\MyStaticValidatorClass',
208+ 'isAuthorValid',
209+ ),
201210 ),
202211 )));
203212 }
204213 }
205214
206- In this case, the static method ``isAuthorValid `` will be called on the
207- ``Acme\BlogBundle\MyStaticValidatorClass `` class. It's passed both the original
208- object being validated (e.g. ``Author ``) as well as the ``ExecutionContextInterface ``::
215+ In this case, the static method ``isAuthorValid `` will be called on
216+ the ``Acme\BlogBundle\MyStaticValidatorClass `` class. It's passed both
217+ the original object being validated (e.g. ``Author ``) as well as the
218+ ``ExecutionContextInterface ``::
209219
210220 namespace Acme\BlogBundle;
211221
@@ -214,17 +224,20 @@ process. Each method can be one of the following formats:
214224
215225 class MyStaticValidatorClass
216226 {
217- public static function isAuthorValid(Author $author, ExecutionContextInterface $context)
218- {
227+ public static function isAuthorValid(
228+ Author $author,
229+ ExecutionContextInterface $context
230+ ) {
219231 // ...
220232 }
221233 }
222234
223235 .. tip ::
224236
225- If you specify your ``Callback `` constraint via PHP, then you also have
226- the option to make your callback either a PHP closure or a non-static
227- callback. It is *not * currently possible, however, to specify a :term: `service `
228- as a constraint. To validate using a service, you should
229- :doc: `create a custom validation constraint </cookbook/validation/custom_constraint >`
230- and add that new constraint to your class.
237+ If you specify your ``Callback `` constraint via PHP, then you also
238+ have the option to make your callback either a PHP closure or a
239+ non-static callback. It is *not * currently possible, however, to
240+ specify a :term: `service ` as a constraint. To validate using a service,
241+ you should :doc: `create a custom validation constraint
242+ </cookbook/validation/custom_constraint>` and add that new constraint
243+ to your class.
0 commit comments