Skip to content

Improved the example about Choice constraint callback #10679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ constraint.
}
}

If the static callback is stored in a different class, for example ``Util``,
you can pass the class name and the method as an array.
If the static callback is stored in a different class, for example ``AppBundle\Entity\Genre``,
you can pass the fully-qualified class and the method as an array.

.. configuration-block::

Expand All @@ -222,7 +222,7 @@ you can pass the class name and the method as an array.
class Author
{
/**
* @Assert\Choice(callback={"Util", "getGenres"})
* @Assert\Choice(callback={"AppBundle\Entity\Genre", "getGenres"})
Copy link
Contributor

@kunicmarko20 kunicmarko20 Nov 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be Genre::class but I guess it is more clear this way

*/
protected $genre;
}
Expand All @@ -233,7 +233,7 @@ you can pass the class name and the method as an array.
AppBundle\Entity\Author:
properties:
genre:
- Choice: { callback: [Util, getGenres] }
- Choice: { callback: [AppBundle\Entity\Genre, getGenres] }

.. code-block:: xml

Expand All @@ -247,7 +247,7 @@ you can pass the class name and the method as an array.
<property name="genre">
<constraint name="Choice">
<option name="callback">
<value>Util</value>
<value>AppBundle\Entity\Genre</value>
<value>getGenres</value>
</option>
</constraint>
Expand All @@ -260,6 +260,7 @@ you can pass the class name and the method as an array.
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;

use AppBundle\Entity\Genre;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

Expand All @@ -270,7 +271,7 @@ you can pass the class name and the method as an array.
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('genre', new Assert\Choice(array(
'callback' => array('Util', 'getGenres'),
'callback' => array(Genre::class, 'getGenres'),
)));
}
}
Expand Down