Skip to content

Commit da05add

Browse files
committed
[Form] Add the docs for EnumType
1 parent a6c6067 commit da05add

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

reference/forms/types.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Form Types Reference
2323
types/color
2424

2525
types/choice
26+
types/enum
2627
types/entity
2728
types/country
2829
types/language

reference/forms/types/enum.rst

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
.. index::
2+
single: Forms; Fields; EnumType
3+
4+
EnumType Field
5+
==============
6+
7+
.. versionadded:: 5.4
8+
9+
The ``EnumType`` form field was introduced in Symfony 5.4.
10+
11+
A multi-purpose field used to allow the user to "choose" one or more options
12+
defined in a `PHP enumeration`_. It extends :doc:`ChoiceType </refernce/forms/types/enum>`
13+
field and defines the same options.
14+
15+
+---------------------------+----------------------------------------------------------------------+
16+
| Rendered as | can be various tags (see below) |
17+
+---------------------------+----------------------------------------------------------------------+
18+
| Default invalid message | The selected choice is invalid. |
19+
+---------------------------+----------------------------------------------------------------------+
20+
| Legacy invalid message | The value {{ value }} is not valid. |
21+
+---------------------------+----------------------------------------------------------------------+
22+
| Parent type | :doc:`ChoiceType </reference/forms/types/choice>` |
23+
+---------------------------+----------------------------------------------------------------------+
24+
| Class | :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\EnumType` |
25+
+---------------------------+----------------------------------------------------------------------+
26+
27+
.. include:: /reference/forms/types/options/_debug_form.rst.inc
28+
29+
Example Usage
30+
-------------
31+
32+
Before using this field, you'll need to have some PHP enumeration (or "enum" for
33+
short) defined somewhere in your application::
34+
35+
// src/Config/TextAlign.php
36+
namespace App\Config;
37+
38+
enum TextAlign
39+
{
40+
case Left = 'Left/Start aligned';
41+
case Center = 'Center/Middle aligned';
42+
case Right = 'Right/End aligned';
43+
}
44+
45+
Instead of using the values of the enumeration in a ``choices`` option, the
46+
``EnumType`` only requires to define the ``class`` option pointing to the enum::
47+
48+
use App\Config\TextAlign;
49+
use Symfony\Component\Form\Extension\Core\Type\EnumType;
50+
// ...
51+
52+
$builder->add('alignment', EnumType::class, ['class' => TextAlign::class]);
53+
54+
This will display a ``<select>`` tag with the three possible values defined in
55+
the ``TextAlign`` enum. Use the ``expanded`` and ``multiple`` options to display
56+
these values as ``<input type="checkbox">`` and ``<input type="radio">``.
57+
58+
Field Options
59+
-------------
60+
61+
class
62+
~~~~~
63+
64+
**type**: ``string`` **default**: (it has no default)
65+
66+
The fully-qualified class name (FQCN) of the PHP enum used to get the values
67+
displayed by this form field.
68+
69+
Inherited Options
70+
-----------------
71+
72+
These options inherit from the :doc:`ChoiceType </reference/forms/types/choice>`:
73+
74+
.. include:: /reference/forms/types/options/error_bubbling.rst.inc
75+
76+
.. include:: /reference/forms/types/options/error_mapping.rst.inc
77+
78+
.. include:: /reference/forms/types/options/expanded.rst.inc
79+
80+
.. include:: /reference/forms/types/options/multiple.rst.inc
81+
82+
.. include:: /reference/forms/types/options/placeholder.rst.inc
83+
84+
.. include:: /reference/forms/types/options/preferred_choices.rst.inc
85+
86+
.. include:: /reference/forms/types/options/choice_type_trim.rst.inc
87+
88+
These options inherit from the :doc:`FormType </reference/forms/types/form>`:
89+
90+
.. include:: /reference/forms/types/options/attr.rst.inc
91+
92+
.. include:: /reference/forms/types/options/data.rst.inc
93+
94+
.. include:: /reference/forms/types/options/disabled.rst.inc
95+
96+
.. include:: /reference/forms/types/options/empty_data_declaration.rst.inc
97+
98+
.. include:: /reference/forms/types/options/empty_data_description.rst.inc
99+
100+
.. include:: /reference/forms/types/options/help.rst.inc
101+
102+
.. include:: /reference/forms/types/options/help_attr.rst.inc
103+
104+
.. include:: /reference/forms/types/options/help_html.rst.inc
105+
106+
.. include:: /reference/forms/types/options/label.rst.inc
107+
108+
.. include:: /reference/forms/types/options/label_attr.rst.inc
109+
110+
.. include:: /reference/forms/types/options/label_format.rst.inc
111+
112+
.. include:: /reference/forms/types/options/mapped.rst.inc
113+
114+
.. include:: /reference/forms/types/options/required.rst.inc
115+
116+
.. include:: /reference/forms/types/options/row_attr.rst.inc
117+
118+
.. _`PHP enumeration`: https://wiki.php.net/rfc/enumerations

reference/forms/types/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Choice Fields
1919
~~~~~~~~~~~~~
2020

2121
* :doc:`ChoiceType </reference/forms/types/choice>`
22+
* :doc:`EnumType </reference/forms/types/enum>`
2223
* :doc:`EntityType </reference/forms/types/entity>`
2324
* :doc:`CountryType </reference/forms/types/country>`
2425
* :doc:`LanguageType </reference/forms/types/language>`

0 commit comments

Comments
 (0)