@@ -15,10 +15,17 @@ option.
15
15
+-------------+------------------------------------------------------------------------------+
16
16
| Options | - `choices `_ |
17
17
| | - `choice_list `_ |
18
+ | | - `choice_loader `_ |
19
+ | | - `choice_label `_ |
20
+ | | - `choice_name `_ |
21
+ | | - `choice_value `_ |
22
+ | | - `choice_attr `_ |
23
+ | | - `choices_as_values `_ |
18
24
| | - `placeholder `_ |
19
25
| | - `expanded `_ |
20
26
| | - `multiple `_ |
21
27
| | - `preferred_choices `_ |
28
+ | | - `group_by `_ |
22
29
+-------------+------------------------------------------------------------------------------+
23
30
| Overridden | - `compound `_ |
24
31
| options | - `empty_data `_ |
@@ -51,7 +58,8 @@ user sees on the form (e.g. ``Male``).
51
58
.. code-block :: php
52
59
53
60
$builder->add('gender', 'choice', array(
54
- 'choices' => array('m' => 'Male', 'f' => 'Female'),
61
+ 'choices' => array('Male' => 'm', 'Female' => 'f'),
62
+ 'choices_as_values' => true,
55
63
'required' => false,
56
64
));
57
65
@@ -61,15 +69,33 @@ of checkboxes depending on the ``expanded`` option::
61
69
62
70
$builder->add('availability', 'choice', array(
63
71
'choices' => array(
64
- 'morning' => 'Morning',
65
- 'afternoon' => 'Afternoon',
66
- 'evening' => 'Evening',
72
+ 'Morning' => 'morning',
73
+ 'Afternoon' => 'afternoon',
74
+ 'Evening' => 'evening',
75
+ ),
76
+ 'choices_as_values' => true,
77
+ 'multiple' => true,
78
+ ));
79
+
80
+ If you rely on your option value attribute (e.g. for JavaScript) you need
81
+ to 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',
67
89
),
90
+ 'choices_as_values' => true,
91
+ 'choice_value' => function ($choice) {
92
+ return $choice;
93
+ },
68
94
'multiple' => true,
69
95
));
70
96
71
- You can also use the ``choice_list `` option, which takes an object that
72
- can specify the choices for your widget .
97
+ You can also use the ``choice_loader `` option, which can be used to load
98
+ the list only partially in cases where a fully-loaded list is not necessary .
73
99
74
100
.. _forms-reference-choice-tags :
75
101
@@ -85,19 +111,59 @@ choices
85
111
86
112
This is the most basic way to specify the choices that should be used
87
113
by 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 ::
89
115
90
116
$builder->add('gender', 'choice', array(
91
- 'choices' => array('m' => 'Male', 'f' => 'Female'),
117
+ 'choices' => array('Male' => 'm', 'Female' => 'f'),
118
+ 'choices_as_values' => true,
92
119
));
93
120
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).
125
+
126
+ choices_as_values
127
+ ~~~~~~~~~~~~~~~~~
95
128
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.
129
+ **type **: ``boolean `` **default **: false
130
+
131
+ .. versionadded :: 2.7
132
+
133
+ The ``choices_as_values `` option of ChoiceType was introduced in Symfony
134
+ 2.7.
135
+
136
+ The ``choices_as_values `` option was introduced to ensure backward compatibility
137
+ with the modified handling of the ``choices `` optio. Being set to ``false ``
138
+ the choices array will be read as values mapping the keys. Setting the option
139
+ to ``true `` will enable the new handling of the choices mapping keys to
140
+ values.
141
+
142
+ * Before 2.7::
143
+
144
+ $builder->add('gender', 'choice', array(
145
+ 'choices' => array('m' => 'Male', 'f' => 'Female'),
146
+ 'choices_as_values' => false,
147
+ ));
148
+
149
+ * Optional since 2.7::
150
+
151
+ $builder->add('gender', 'choice', array(
152
+ 'choices' => array('Male' => 'm', 'Female' => 'f'),
153
+ 'choices_as_values' => true,
154
+ ));
155
+
156
+ * Default for 3.0::
157
+
158
+ $builder->add('gender', 'choice', array(
159
+ 'choices' => array('Male' => 'm', 'Female' => 'f'),
160
+ ));
161
+
162
+ .. caution ::
163
+
164
+ The ``choices_as_values `` option will be removed in Symfony 3.0,
165
+ where the choices will be passed in the values of the ``choices ``
166
+ option by default.
101
167
102
168
choice_list
103
169
~~~~~~~~~~~
@@ -141,6 +207,19 @@ But don't be confused! If ``Full`` is selected (value ``0`` in HTML), ``1``
141
207
will be returned in your form. If ``Almost empty `` is selected (value ``2 ``
142
208
in HTML), ``0.1 `` will be returned.
143
209
210
+ choice_loader
211
+ ~~~~~~~~~~~~~
212
+
213
+ .. versionadded :: 2.7
214
+
215
+ The ``choice_loader `` option of ChoiceType was introduced in Symfony
216
+ 2.7.
217
+
218
+ The choice loader can be used to load the list only partially in cases where
219
+ a fully-loaded list is not necessary.
220
+
221
+ **type **: :class: `Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface `
222
+
144
223
.. include :: /reference/forms/types/options/placeholder.rst.inc
145
224
146
225
.. include :: /reference/forms/types/options/expanded.rst.inc
@@ -149,6 +228,16 @@ in HTML), ``0.1`` will be returned.
149
228
150
229
.. include :: /reference/forms/types/options/preferred_choices.rst.inc
151
230
231
+ .. include :: /reference/forms/types/options/choice_label.rst.inc
232
+
233
+ .. include :: /reference/forms/types/options/choice_name.rst.inc
234
+
235
+ .. include :: /reference/forms/types/options/choice_value.rst.inc
236
+
237
+ .. include :: /reference/forms/types/options/choice_attr.rst.inc
238
+
239
+ .. include :: /reference/forms/types/options/group_by.rst.inc
240
+
152
241
Overridden Options
153
242
------------------
154
243
0 commit comments