Skip to content

Commit a6d7053

Browse files
committed
minor symfony#10679 Improved the example about Choice constraint callback (javiereguiluz)
This PR was merged into the 2.8 branch. Discussion ---------- Improved the example about Choice constraint callback I don't know if this is 100% correct, but in a real Symfony project, the `callback` without the FQCN didn't work (at least when using the PHP annotations). Commits ------- dece243 Improved the example about Choice constraint callback
2 parents 88939de + dece243 commit a6d7053

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

reference/constraints/Choice.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ constraint.
207207
}
208208
}
209209
210-
If the static callback is stored in a different class, for example ``Util``,
211-
you can pass the class name and the method as an array.
210+
If the static callback is stored in a different class, for example ``AppBundle\Entity\Genre``,
211+
you can pass the fully-qualified class and the method as an array.
212212

213213
.. configuration-block::
214214

@@ -222,7 +222,7 @@ you can pass the class name and the method as an array.
222222
class Author
223223
{
224224
/**
225-
* @Assert\Choice(callback={"Util", "getGenres"})
225+
* @Assert\Choice(callback={"AppBundle\Entity\Genre", "getGenres"})
226226
*/
227227
protected $genre;
228228
}
@@ -233,7 +233,7 @@ you can pass the class name and the method as an array.
233233
AppBundle\Entity\Author:
234234
properties:
235235
genre:
236-
- Choice: { callback: [Util, getGenres] }
236+
- Choice: { callback: [AppBundle\Entity\Genre, getGenres] }
237237
238238
.. code-block:: xml
239239
@@ -247,7 +247,7 @@ you can pass the class name and the method as an array.
247247
<property name="genre">
248248
<constraint name="Choice">
249249
<option name="callback">
250-
<value>Util</value>
250+
<value>AppBundle\Entity\Genre</value>
251251
<value>getGenres</value>
252252
</option>
253253
</constraint>
@@ -260,6 +260,7 @@ you can pass the class name and the method as an array.
260260
// src/AppBundle/Entity/Author.php
261261
namespace AppBundle\Entity;
262262
263+
use AppBundle\Entity\Genre;
263264
use Symfony\Component\Validator\Mapping\ClassMetadata;
264265
use Symfony\Component\Validator\Constraints as Assert;
265266
@@ -270,7 +271,7 @@ you can pass the class name and the method as an array.
270271
public static function loadValidatorMetadata(ClassMetadata $metadata)
271272
{
272273
$metadata->addPropertyConstraint('genre', new Assert\Choice(array(
273-
'callback' => array('Util', 'getGenres'),
274+
'callback' => array(Genre::class, 'getGenres'),
274275
)));
275276
}
276277
}

0 commit comments

Comments
 (0)