From 9b234623a59adf78f7bebc8b4da8aee3db8a3e7b Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Thu, 27 Dec 2018 12:17:52 +0100 Subject: [PATCH 1/2] validator : choice constraint : explain how to supply choices from a constant --- reference/constraints/Choice.rst | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst index 9c41bca90d3..c666c6e625c 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -210,7 +210,7 @@ constraint. } } -If the callback is stored in a different class and is static, for example ``AppBundle\Entity\Genre``, +If the callback is defined in a different class and is static, for example ``AppBundle\Entity\Genre``, you can pass the class name and the method as an array. .. configuration-block:: @@ -279,6 +279,35 @@ you can pass the class name and the method as an array. } } +Supplying the Choices from an Array Constant +-------------------------------------------- + +You can also directly provide a constant containing an array to the ``choices`` option in the annotation:: + + // src/AppBundle/Entity/Author.php + namespace AppBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + const GENRES = ['action', 'comedy']; + + /** + * @Assert\Choice(choices=Author::GENRES) + */ + protected $genre; + } + +.. warning:: + + The constant in the option is used without quotes. + +.. note:: + + If the constant is defined in a different class, you can pass the fully qualified class name. + + Available Options ----------------- From 65a2dc8b88da5fa49f4d58659406e35a4086bf93 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Tue, 19 Feb 2019 08:35:33 +0100 Subject: [PATCH 2/2] define choices with a constant in the main example --- reference/constraints/Choice.rst | 35 +++++--------------------------- 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst index c666c6e625c..521fb5e8011 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -46,13 +46,17 @@ If your valid choice list is simple, you can pass them in directly via the class Author { + const GENRES = ['fiction', 'non-fiction']; + /** * @Assert\Choice({"New York", "Berlin", "Tokyo"}) */ protected $city; /** - * @Assert\Choice(choices={"fiction", "non-fiction"}, message="Choose a valid genre.") + * You can also directly provide an array constant to the "choices" option in the annotation + * + * @Assert\Choice(choices=Author::GENRES, message="Choose a valid genre.") */ protected $genre; } @@ -279,35 +283,6 @@ you can pass the class name and the method as an array. } } -Supplying the Choices from an Array Constant --------------------------------------------- - -You can also directly provide a constant containing an array to the ``choices`` option in the annotation:: - - // src/AppBundle/Entity/Author.php - namespace AppBundle\Entity; - - use Symfony\Component\Validator\Constraints as Assert; - - class Author - { - const GENRES = ['action', 'comedy']; - - /** - * @Assert\Choice(choices=Author::GENRES) - */ - protected $genre; - } - -.. warning:: - - The constant in the option is used without quotes. - -.. note:: - - If the constant is defined in a different class, you can pass the fully qualified class name. - - Available Options -----------------