@@ -58,6 +58,27 @@ If your valid choice list is simple, you can pass them in directly via the
58
58
protected $genre;
59
59
}
60
60
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
+
61
82
.. code-block :: yaml
62
83
63
84
# config/validator/validation.yaml
@@ -160,6 +181,19 @@ constraint.
160
181
protected $genre;
161
182
}
162
183
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
+
163
197
.. code-block :: yaml
164
198
165
199
# config/validator/validation.yaml
@@ -225,6 +259,19 @@ you can pass the class name and the method as an array.
225
259
protected $genre;
226
260
}
227
261
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
+
228
275
.. code-block :: yaml
229
276
230
277
# config/validator/validation.yaml
0 commit comments