|
4 | 4 | ``choice`` Field Type |
5 | 5 | ===================== |
6 | 6 |
|
7 | | -See :class:`Symfony\\Component\\Form\\Type\\ChoiceType`. |
| 7 | +A multi-purpose field used to allow the user to "choose" one or more options. |
| 8 | +It can be rendered as a ``select`` tag, radio tags, or checkboxes. |
| 9 | + |
| 10 | +To use this field, you must specify *either* the ``choice_list`` or ``choices`` |
| 11 | +option. |
| 12 | + |
| 13 | +============ ====== |
| 14 | +Rendered as can be various tags (see below) |
| 15 | +Options ``multiple``, ``expanded``, ``choices``, ``choice_list``, ``preferred_choices``, ``required``, ``label``, ``read_only``, ``error_bubbling`` |
| 16 | +Parent type :doc:`form</reference/forms/types/form>` (if expanded), ``field`` otherwise |
| 17 | +Class :class:`Symfony\\Component\\Form\\Type\\ChoiceType` |
| 18 | +============ ====== |
| 19 | + |
| 20 | +HTML Element Rendering |
| 21 | +---------------------- |
| 22 | + |
| 23 | +This field may be rendered as one of several different HTML fields, depending |
| 24 | +on the ``expanded`` and ``multiple`` options: |
| 25 | + |
| 26 | +============ ============= =========== |
| 27 | +**expanded** **multiple** **element type** |
| 28 | +false false ``select`` tag |
| 29 | +false true ``select`` tag (with ``multiple`` attribute) |
| 30 | +true false ``input`` ``radio`` tags |
| 31 | +true true ``input`` ``checkbox`` tags |
| 32 | +============ ============= =========== |
| 33 | + |
| 34 | +Options |
| 35 | +------- |
| 36 | + |
| 37 | +* ``multiple`` [type: Boolean, default: false] |
| 38 | + If true, the user will be able to select multiple options (as opposed |
| 39 | + to choosing just one option). Depending on the value of the ``expanded`` |
| 40 | + option, this will render either a select tag or checkboxes if true and |
| 41 | + a select tag or radio buttons if false. |
| 42 | + |
| 43 | +* ``expanded`` [type: Boolean, default: false] |
| 44 | + If set to true, radio buttons or checkboxes will be rendered (depending |
| 45 | + on the ``multiple`` value). If false, a select element will be rendered. |
| 46 | + |
| 47 | +* ``choices`` [type: array] |
| 48 | + This is the most basic way to specify the choices that should be used |
| 49 | + by this field. The ``choices`` option is an array, where the array key |
| 50 | + is the item value and the array value is the item's label: |
| 51 | + |
| 52 | + .. code-block:: php |
| 53 | + |
| 54 | + $builder->add('gender', 'choice', array( |
| 55 | + 'choices' => array('m' => 'Male', 'f' => 'Female') |
| 56 | + )); |
| 57 | +
|
| 58 | +* ``choice_list`` [type: ``Symfony\Component\Form\ChoiceList\ChoiceListInterface``] |
| 59 | + This is one way of specifying the options to be used for this field. |
| 60 | + The ``choice_list`` option must be an instance of the ``ChoiceListInterface``. |
| 61 | + For more advanced cases, a custom class that implements the interface |
| 62 | + can be created to supply the choices. |
| 63 | + |
| 64 | +* ``preferred_choices`` [type: array] |
| 65 | + If this option is specified, then a sub-set of the total number of options |
| 66 | + will be moved to the top of the select menu. The following would move |
| 67 | + the "Baz" option to the top: |
| 68 | + |
| 69 | + .. code-block:: php |
| 70 | + |
| 71 | + $builder->add('foo_choices', 'choice', array( |
| 72 | + 'choices' => array('foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz'), |
| 73 | + 'preferred_choices' => array('baz' => 'Baz'), |
| 74 | + )); |
| 75 | +
|
| 76 | +.. include:: /reference/forms/types/options/required.rst.inc |
| 77 | + |
| 78 | +.. include:: /reference/forms/types/options/label.rst.inc |
| 79 | + |
| 80 | +.. include:: /reference/forms/types/options/read_only.rst.inc |
| 81 | + |
| 82 | +.. include:: /reference/forms/types/options/error_bubbling.rst.inc |
0 commit comments