Skip to content

Commit b94e3f7

Browse files
committed
[#3012] Tweaks to Callback changes - most thanks to the careful eye of @xabbuh
1 parent 9b04e53 commit b94e3f7

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

reference/constraints/Callback.rst

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ those errors should be attributed::
114114

115115
// check if the name is actually a fake name
116116
if (in_array($this->getFirstName(), $fakeNames)) {
117-
$context->addViolationAt('firstName', 'This name sounds totally fake!', array(), null);
117+
$context->addViolationAt(
118+
'firstName',
119+
'This name sounds totally fake!',
120+
array(),
121+
null
122+
);
118123
}
119124
}
120125
}
@@ -123,7 +128,7 @@ Static Callbacks
123128
----------------
124129

125130
You can also use the constraint with static methods. Since static methods don't
126-
have access to the object instance, they receive the object as first argument::
131+
have access to the object instance, they receive the object as the first argument::
127132

128133
public static function validate($object, ExecutionContextInterface $context)
129134
{
@@ -132,7 +137,12 @@ have access to the object instance, they receive the object as first argument::
132137

133138
// check if the name is actually a fake name
134139
if (in_array($object->getFirstName(), $fakeNames)) {
135-
$context->addViolationAt('firstName', 'This name sounds totally fake!', array(), null);
140+
$context->addViolationAt(
141+
'firstName',
142+
'This name sounds totally fake!',
143+
array(),
144+
null
145+
);
136146
}
137147
}
138148

@@ -142,7 +152,7 @@ External Callbacks and Closures
142152
If you want to execute a static callback method that is not located in the class
143153
of the validated object, you can configure the constraint to invoke an array
144154
callable as supported by PHP's :phpfunction:`call_user_func` function. Suppose
145-
your validation function is `Vendor\Package\Validator::validate()`::
155+
your validation function is ``Vendor\Package\Validator::validate()``::
146156

147157
namespace Vendor\Package;
148158

@@ -156,7 +166,7 @@ your validation function is `Vendor\Package\Validator::validate()`::
156166
}
157167
}
158168

159-
You can then use the following configuration to invoke this validator::
169+
You can then use the following configuration to invoke this validator:
160170

161171
.. configuration-block::
162172

@@ -173,7 +183,6 @@ You can then use the following configuration to invoke this validator::
173183
namespace Acme\BlogBundle\Entity;
174184
175185
use Symfony\Component\Validator\Constraints as Assert;
176-
use Symfony\Component\Validator\ExecutionContextInterface;
177186
178187
/**
179188
* @Assert\Callback({"Vendor\Package\Validator", "validate"})
@@ -212,17 +221,17 @@ You can then use the following configuration to invoke this validator::
212221
{
213222
$metadata->addConstraint(new Assert\Callback(array(
214223
'Vendor\Package\Validator',
215-
'validate'
224+
'validate',
216225
)));
217226
}
218227
}
219228
220229
.. note::
221230

222-
The Callback constraint does *not* support global callback functions or
223-
It is *not* possible to specify a global function or a :term:`service`
224-
method as callback. To validate using a service, you should
225-
:doc:`create a custom validation constraint</cookbook/validation/custom_constraint>`
231+
The Callback constraint does *not* support global callback functions nor
232+
is it possible to specify a global function or a :term:`service` method
233+
as callback. To validate using a service, you should
234+
:doc:`create a custom validation constraint </cookbook/validation/custom_constraint>`
226235
and add that new constraint to your class.
227236

228237
When configuring the constraint via PHP, you can also pass a closure to the
@@ -252,20 +261,20 @@ Options
252261
callback
253262
~~~~~~~~
254263

255-
**type**: ``string``, ``array`` or ``Closure`` [:ref:`default option<validation-default-option>`]
264+
**type**: ``string``, ``array`` or ``Closure`` [:ref:`default option <validation-default-option>`]
256265

257266
The callback option accepts three different formats for specifying the
258267
callback method:
259268

260-
* A **string** containing the name of a concrete or static method.
269+
* A **string** containing the name of a concrete or static method;
261270

262-
* An array callable with the format ``array('<Class>', '<method>')``.
271+
* An array callable with the format ``array('<Class>', '<method>')``;
263272

264273
* A closure.
265274

266275
Concrete callbacks receive an :class:`Symfony\\Component\\Validator\\ExecutionContextInterface`
267276
instance as only argument.
268277

269-
Static or closure callbacks receive the validated object as first argument
278+
Static or closure callbacks receive the validated object as the first argument
270279
and the :class:`Symfony\\Component\\Validator\\ExecutionContextInterface`
271-
instance as second argument.
280+
instance as the second argument.

0 commit comments

Comments
 (0)