@@ -58,6 +58,27 @@ If your valid choice list is simple, you can pass them in directly via the
5858 protected $genre;
5959 }
6060
61+ .. code-block :: php-attributes
62+
63+ // src/Entity/Author.php
64+ namespace App\Entity;
65+
66+ use Symfony\Component\Validator\Constraints as Assert;
67+
68+ class Author
69+ {
70+ const GENRES = ['fiction', 'non-fiction'];
71+
72+ #[Assert\Choice(["New York", "Berlin", "Tokyo"])]
73+ protected $city;
74+
75+ /**
76+ * You can also directly provide an array constant to the "choices" option in the annotation
77+ */
78+ #[Assert\Choice(choices: Conference::GENRES, message: "Choose a valid genre.")]
79+ protected $genre;
80+ }
81+
6182 .. code-block :: yaml
6283
6384 # config/validator/validation.yaml
@@ -160,6 +181,19 @@ constraint.
160181 protected $genre;
161182 }
162183
184+ .. code-block :: php-attributes
185+
186+ // src/Entity/Author.php
187+ namespace App\Entity;
188+
189+ use Symfony\Component\Validator\Constraints as Assert;
190+
191+ class Author
192+ {
193+ #[Assert\Choice(callback: "getGenres")]
194+ protected $genre;
195+ }
196+
163197 .. code-block :: yaml
164198
165199 # config/validator/validation.yaml
@@ -225,6 +259,19 @@ you can pass the class name and the method as an array.
225259 protected $genre;
226260 }
227261
262+ .. code-block :: php-attributes
263+
264+ // src/Entity/Author.php
265+ namespace App\Entity;
266+
267+ use Symfony\Component\Validator\Constraints as Assert;
268+
269+ class Author
270+ {
271+ #[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])]
272+ protected $genre;
273+ }
274+
228275 .. code-block :: yaml
229276
230277 # config/validator/validation.yaml
0 commit comments