|
| 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 the :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. This enum has to be of type |
| 34 | +"backed enum", where each keyword defines a scalar value such a string:: |
| 35 | + |
| 36 | + // src/Config/TextAlign.php |
| 37 | + namespace App\Config; |
| 38 | + |
| 39 | + enum TextAlign |
| 40 | + { |
| 41 | + case Left = 'Left/Start aligned'; |
| 42 | + case Center = 'Center/Middle aligned'; |
| 43 | + case Right = 'Right/End aligned'; |
| 44 | + } |
| 45 | + |
| 46 | +Instead of using the values of the enumeration in a ``choices`` option, the |
| 47 | +``EnumType`` only requires to define the ``class`` option pointing to the enum:: |
| 48 | + |
| 49 | + use App\Config\TextAlign; |
| 50 | + use Symfony\Component\Form\Extension\Core\Type\EnumType; |
| 51 | + // ... |
| 52 | + |
| 53 | + $builder->add('alignment', EnumType::class, ['class' => TextAlign::class]); |
| 54 | + |
| 55 | +This will display a ``<select>`` tag with the three possible values defined in |
| 56 | +the ``TextAlign`` enum. Use the ``expanded`` and ``multiple`` options to display |
| 57 | +these values as ``<input type="checkbox">`` and ``<input type="radio">``. |
| 58 | + |
| 59 | +Field Options |
| 60 | +------------- |
| 61 | + |
| 62 | +class |
| 63 | +~~~~~ |
| 64 | + |
| 65 | +**type**: ``string`` **default**: (it has no default) |
| 66 | + |
| 67 | +The fully-qualified class name (FQCN) of the PHP enum used to get the values |
| 68 | +displayed by this form field. |
| 69 | + |
| 70 | +Inherited Options |
| 71 | +----------------- |
| 72 | + |
| 73 | +These options inherit from the :doc:`ChoiceType </reference/forms/types/choice>`: |
| 74 | + |
| 75 | +.. include:: /reference/forms/types/options/error_bubbling.rst.inc |
| 76 | + |
| 77 | +.. include:: /reference/forms/types/options/error_mapping.rst.inc |
| 78 | + |
| 79 | +.. include:: /reference/forms/types/options/expanded.rst.inc |
| 80 | + |
| 81 | +.. include:: /reference/forms/types/options/multiple.rst.inc |
| 82 | + |
| 83 | +.. include:: /reference/forms/types/options/placeholder.rst.inc |
| 84 | + |
| 85 | +.. include:: /reference/forms/types/options/preferred_choices.rst.inc |
| 86 | + |
| 87 | +.. include:: /reference/forms/types/options/choice_type_trim.rst.inc |
| 88 | + |
| 89 | +These options inherit from the :doc:`FormType </reference/forms/types/form>`: |
| 90 | + |
| 91 | +.. include:: /reference/forms/types/options/attr.rst.inc |
| 92 | + |
| 93 | +.. include:: /reference/forms/types/options/data.rst.inc |
| 94 | + |
| 95 | +.. include:: /reference/forms/types/options/disabled.rst.inc |
| 96 | + |
| 97 | +.. include:: /reference/forms/types/options/empty_data_declaration.rst.inc |
| 98 | + |
| 99 | +.. include:: /reference/forms/types/options/empty_data_description.rst.inc |
| 100 | + |
| 101 | +.. include:: /reference/forms/types/options/help.rst.inc |
| 102 | + |
| 103 | +.. include:: /reference/forms/types/options/help_attr.rst.inc |
| 104 | + |
| 105 | +.. include:: /reference/forms/types/options/help_html.rst.inc |
| 106 | + |
| 107 | +.. include:: /reference/forms/types/options/label.rst.inc |
| 108 | + |
| 109 | +.. include:: /reference/forms/types/options/label_attr.rst.inc |
| 110 | + |
| 111 | +.. include:: /reference/forms/types/options/label_format.rst.inc |
| 112 | + |
| 113 | +.. include:: /reference/forms/types/options/mapped.rst.inc |
| 114 | + |
| 115 | +.. include:: /reference/forms/types/options/required.rst.inc |
| 116 | + |
| 117 | +.. include:: /reference/forms/types/options/row_attr.rst.inc |
| 118 | + |
| 119 | +.. _`PHP enumeration`: https://wiki.php.net/rfc/enumerations |
0 commit comments