@@ -15,10 +15,17 @@ option.
1515+-------------+------------------------------------------------------------------------------+
1616| Options | - `choices `_ |
1717| | - `choice_list `_ |
18+ | | - `choice_loader `_ |
19+ | | - `choice_label `_ |
20+ | | - `choice_name `_ |
21+ | | - `choice_value `_ |
22+ | | - `choice_attr `_ |
23+ | | - `choices_as_values `_ |
1824| | - `placeholder `_ |
1925| | - `expanded `_ |
2026| | - `multiple `_ |
2127| | - `preferred_choices `_ |
28+ | | - `group_by `_ |
2229+-------------+------------------------------------------------------------------------------+
2330| Overridden | - `compound `_ |
2431| options | - `empty_data `_ |
@@ -51,7 +58,8 @@ user sees on the form (e.g. ``Male``).
5158.. code-block :: php
5259
5360 $builder->add('gender', 'choice', array(
54- 'choices' => array('m' => 'Male', 'f' => 'Female'),
61+ 'choices' => array('Male' => 'm', 'Female' => 'f'),
62+ 'choices_as_values' => true,
5563 'required' => false,
5664 ));
5765
@@ -61,15 +69,33 @@ of checkboxes depending on the ``expanded`` option::
6169
6270 $builder->add('availability', 'choice', array(
6371 'choices' => array(
64- 'morning ' => 'Morning ',
65- 'afternoon ' => 'Afternoon ',
66- 'evening ' => 'Evening ',
72+ 'Morning ' => 'morning ',
73+ 'Afternoon ' => 'afternoon ',
74+ 'Evening ' => 'evening ',
6775 ),
76+ 'choices_as_values' => true,
6877 'multiple' => true,
6978 ));
7079
71- You can also use the ``choice_list `` option, which takes an object that
72- can specify the choices for your widget.
80+ If you rely on your option value attribute (e.g. for JavaScript) you need to
81+ set ``choice_value ``, otherwise the option values will be mapped to integer
82+ values::
83+
84+ $builder->add('availability', 'choice', array(
85+ 'choices' => array(
86+ 'Morning' => 'morning',
87+ 'Afternoon' => 'afternoon',
88+ 'Evening' => 'evening',
89+ ),
90+ 'choices_as_values' => true,
91+ 'choice_value' => function ($choice) {
92+ return $choice;
93+ },
94+ 'multiple' => true,
95+ ));
96+
97+ You can also use the ``choice_loader `` option, which can be used to load the
98+ list only partially in cases where a fully-loaded list is not necessary.
7399
74100.. _forms-reference-choice-tags :
75101
@@ -85,19 +111,23 @@ choices
85111
86112This is the most basic way to specify the choices that should be used
87113by this field. The ``choices `` option is an array, where the array key
88- is the item value and the array value is the item's label ::
114+ is the item's label and the array value is the item's value ::
89115
90116 $builder->add('gender', 'choice', array(
91- 'choices' => array('m' => 'Male', 'f' => 'Female'),
117+ 'choices' => array('Male' => 'm', 'Female' => 'f'),
118+ 'choices_as_values' => true,
92119 ));
93120
94- .. tip ::
121+ .. versionadded :: 2.7
122+ Symfony 2.7 introduced the option to flip the ``choices `` array to be
123+ able to use values that are not integers or strings (but e.g. floats
124+ or booleans).
95125
96- When the values to choose from are not integers or strings (but e.g.
97- floats or booleans), you should use the ` choice_list `_ option instead.
98- With this option you are able to keep the original data format which
99- is important to ensure that the user input is validated properly and
100- useless database updates caused by a data type mismatch are avoided .
126+ .. caution ::
127+
128+ The `` choices_as_values `` option will be removed in Symfony 3.0, where
129+ the choices will be passed in the values of the `` choices `` option by
130+ default .
101131
102132choice_list
103133~~~~~~~~~~~
@@ -149,6 +179,16 @@ in HTML), ``0.1`` will be returned.
149179
150180.. include :: /reference/forms/types/options/preferred_choices.rst.inc
151181
182+ .. include :: /reference/forms/types/options/choice_label.rst.inc
183+
184+ .. include :: /reference/forms/types/options/choice_name.rst.inc
185+
186+ .. include :: /reference/forms/types/options/choice_value.rst.inc
187+
188+ .. include :: /reference/forms/types/options/choice_attr.rst.inc
189+
190+ .. include :: /reference/forms/types/options/group_by.rst.inc
191+
152192Overridden Options
153193------------------
154194
0 commit comments