@@ -10,6 +10,7 @@ an array of items is one of those valid choices.
10
10
+----------------+----------------------------------------------------------------------+
11
11
| Options | - `choices `_ |
12
12
| | - `callback `_ |
13
+ | | - `enum `_ |
13
14
| | - `multiple `_ |
14
15
| | - `min `_ |
15
16
| | - `max `_ |
@@ -279,6 +280,92 @@ you can pass the class name and the method as an array.
279
280
}
280
281
}
281
282
283
+ Supplying the Choices with a enum class
284
+ ---------------------------------------
285
+
286
+ You can also use a class constants as source for your options. This is useful
287
+ if you are emulating enum structure via class constants.
288
+
289
+ .. code-block :: php
290
+
291
+ // src/AppBundle/Enum/Gender.php
292
+ namespace AppBundle\Enum;
293
+
294
+ final class Gender
295
+ {
296
+ const MALE = 'male';
297
+ const FEMALE = 'female';
298
+ const OTHER = 'other';
299
+
300
+ private function __construct()
301
+ {
302
+ /* noop */
303
+ }
304
+ }
305
+
306
+ .. configuration-block ::
307
+
308
+ .. code-block :: php-annotations
309
+
310
+ // src/AppBundle/Entity/Author.php
311
+ namespace AppBundle\Entity;
312
+
313
+ use Symfony\Component\Validator\Constraints as Assert;
314
+
315
+ class Author
316
+ {
317
+ /**
318
+ * @Assert\Choice(enum = "\\AppBundle\\Enum\\Gender")
319
+ */
320
+ protected $gender;
321
+ }
322
+
323
+ .. code-block :: yaml
324
+
325
+ # src/AppBundle/Resources/config/validation.yml
326
+ AppBundle\Entity\Author :
327
+ properties :
328
+ gender :
329
+ - Choice : { enum: '\\AppBundle\\Enum\\Gender' }
330
+
331
+ .. code-block :: xml
332
+
333
+ <!-- src/AppBundle/Resources/config/validation.xml -->
334
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
335
+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
336
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
337
+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
338
+
339
+ <class name =" AppBundle\Entity\Author" >
340
+ <property name =" gender" >
341
+ <constraint name =" Choice" >
342
+ <option name =" enum" >\AppBundle\Enum\Gender</option >
343
+ </constraint >
344
+ </property >
345
+ </class >
346
+ </constraint-mapping >
347
+
348
+ .. code-block :: php
349
+
350
+ // src/AppBundle/EntityAuthor.php
351
+ namespace AppBundle\Entity;
352
+
353
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
354
+ use Symfony\Component\Validator\Constraints as Assert;
355
+
356
+ class Author
357
+ {
358
+ protected $gender;
359
+
360
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
361
+ {
362
+ $metadata->addPropertyConstraint('gender', new Assert\Choice(array(
363
+ 'enum' => '\\AppBundle\\Enum\\Gender',
364
+ )));
365
+ }
366
+ }
367
+
368
+
282
369
Available Options
283
370
-----------------
284
371
@@ -287,7 +374,7 @@ choices
287
374
288
375
**type **: ``array `` [:ref: `default option <validation-default-option >`]
289
376
290
- A required option (unless `callback `_ is specified) - this is the array
377
+ A required option (unless `callback `_ or ` enum `_ is specified) - this is the array
291
378
of options that should be considered in the valid set. The input value
292
379
will be matched against this array.
293
380
@@ -300,6 +387,15 @@ This is a callback method that can be used instead of the `choices`_ option
300
387
to return the choices array. See
301
388
`Supplying the Choices with a Callback Function `_ for details on its usage.
302
389
390
+ enum
391
+ ~~~~
392
+
393
+ **type **: ``string|object ``
394
+
395
+ This is a class/object that emulates enumerator structure whose constants
396
+ can be used instead of the `choices `_ option to provide the choices array. See
397
+ `Supplying the Choices with a enum class `_ for details on its usage.
398
+
303
399
multiple
304
400
~~~~~~~~
305
401
0 commit comments