Skip to content

Commit 71bd36f

Browse files
committed
[Validator] New feature: Choice validator is able to use class constants as source of choices from now on.
1 parent c45a7f2 commit 71bd36f

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

reference/constraints/Choice.rst

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ an array of items is one of those valid choices.
1010
+----------------+----------------------------------------------------------------------+
1111
| Options | - `choices`_ |
1212
| | - `callback`_ |
13+
| | - `enum`_ |
1314
| | - `multiple`_ |
1415
| | - `min`_ |
1516
| | - `max`_ |
@@ -279,6 +280,92 @@ you can pass the class name and the method as an array.
279280
}
280281
}
281282
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+
282369
Available Options
283370
-----------------
284371

@@ -287,7 +374,7 @@ choices
287374

288375
**type**: ``array`` [:ref:`default option <validation-default-option>`]
289376

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
291378
of options that should be considered in the valid set. The input value
292379
will be matched against this array.
293380

@@ -300,6 +387,15 @@ This is a callback method that can be used instead of the `choices`_ option
300387
to return the choices array. See
301388
`Supplying the Choices with a Callback Function`_ for details on its usage.
302389

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+
303399
multiple
304400
~~~~~~~~
305401

0 commit comments

Comments
 (0)