1
1
.. index ::
2
2
single: Forms; Fields; choice
3
3
4
- choice Field Type
5
- =================
4
+ choice Field Type (select drop-downs, radio buttons & checkboxes)
5
+ =================================================================
6
6
7
7
A multi-purpose field used to allow the user to "choose" one or more options.
8
8
It can be rendered as a ``select `` tag, radio buttons, or checkboxes.
9
9
10
- To use this field, you must specify *either * the ``choice_list `` or ``choices ``
11
- option.
10
+ To use this field, you must specify *either * ``choices `` or ``choice_loader `` option.
12
11
13
12
+-------------+------------------------------------------------------------------------------+
14
13
| Rendered as | can be various tags (see below) |
15
14
+-------------+------------------------------------------------------------------------------+
16
15
| Options | - `choices `_ |
17
- | | - `choice_list `_ |
16
+ | | - `choices_as_values `_ |
18
17
| | - `choice_loader `_ |
19
18
| | - `choice_label `_ |
20
19
| | - `choice_attr `_ |
21
- | | - `choices_as_values `_ |
22
20
| | - `placeholder `_ |
23
21
| | - `expanded `_ |
24
22
| | - `multiple `_ |
25
23
| | - `preferred_choices `_ |
26
24
| | - `group_by `_ |
27
- | | - `choice_name `_ |
28
25
| | - `choice_value `_ |
26
+ | | - `choice_name `_ |
27
+ | | - `choice_list `_ (deprecated) |
29
28
+-------------+------------------------------------------------------------------------------+
30
29
| Overridden | - `compound `_ |
31
30
| options | - `empty_data `_ |
@@ -51,56 +50,112 @@ Example Usage
51
50
-------------
52
51
53
52
The easiest way to use this field is to specify the choices directly via
54
- the ``choices `` option. The key of the array becomes the value that's actually
55
- set on your underlying object (e.g. ``m ``), while the value is what the
56
- user sees on the form (e.g. ``Male ``).
57
-
58
- .. code-block :: php
53
+ the ``choices `` option::
59
54
60
- $builder->add('gender', 'choice', array(
61
- 'choices' => array('Male' => 'm', 'Female' => 'f'),
55
+ $builder->add('isAttending', 'choice', array(
56
+ 'choices' => array(
57
+ 'Maybe' => null,
58
+ 'Yes' => true,
59
+ 'No' => false,
60
+ ),
61
+ // *this line is important*
62
62
'choices_as_values' => true,
63
- 'required' => false,
64
63
));
65
64
66
- By setting ``multiple `` to true, you can allow the user to choose multiple
67
- values. The widget will be rendered as a multiple ``select `` tag or a series
68
- of checkboxes depending on the ``expanded `` option::
65
+ This will create a ``select `` drop-down like this:
69
66
70
- $builder->add('availability', 'choice', array(
71
- 'choices' => array(
72
- 'Morning' => 'morning',
73
- 'Afternoon' => 'afternoon',
74
- 'Evening' => 'evening',
75
- ),
76
- 'choices_as_values' => true,
77
- 'multiple' => true,
78
- ));
67
+ .. image :: /images/reference/form/choice-example1.png
68
+ :align: center
69
+ :scale: 50
70
+ :width: 350
79
71
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::
72
+ If the user selects ``No ``, the form will return ``false `` for this field. Similarly,
73
+ if the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
74
+ In other words, the **value ** of each item is the value you want to get/set in PHP
75
+ code, while the **key ** is what will be shown to the user.
83
76
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
- ));
77
+ .. caution ::
96
78
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.
79
+ The ``choices_as_values `` *must * be set to ``true `` in all cases. This activates
80
+ the "new" choice type API, which was introduced in Symfony 2.7. If you omit this
81
+ option (or set it to ``false ``), you'll activate the old API, which is deprecated
82
+ and will be removed in 3.0. To read about the old API, read an older version of
83
+ the docs.
84
+
85
+ Advanced Example (with Objects!)
86
+ --------------------------------
87
+
88
+ This field has a *lot * of options and most control how the field is displayed. In
89
+ this example, the underlying data is some ``Category `` object that has a ``getName() ``
90
+ method::
91
+
92
+ $builder
93
+ ->add('category', 'choice', [
94
+ 'choices' => [
95
+ new Category('Cat1'),
96
+ new Category('Cat2'),
97
+ new Category('Cat3'),
98
+ new Category('Cat4'),
99
+ ],
100
+ 'choices_as_values' => true,
101
+ 'choice_label' => function($val, $key, $index) {
102
+ /** @var Category $val */
103
+ return strtoupper($val->getName());
104
+ },
105
+ 'choice_attr' => function($val, $key, $index) {
106
+ return ['class' => 'category_'.strtolower($val->getName())];
107
+ },
108
+ 'group_by' => function($val, $key, $index) {
109
+ // randomly assign things into 2 groups
110
+ return rand(0, 1) == 1 ? 'Group A' : 'Group B'
111
+ },
112
+ 'preferred_choices' => function($val, $key, $index) {
113
+ return $val->getName() == 'Cat2' || $val->getName() == 'Cat3';
114
+ },
115
+ ]);
116
+
117
+ You can also customize the `choice_name `_ and `choice_value `_ of each choice if
118
+ you need further HTML customization.
99
119
100
120
.. _forms-reference-choice-tags :
101
121
102
122
.. include :: /reference/forms/types/options/select_how_rendered.rst.inc
103
123
124
+ Customizing each Option's Text (Label)
125
+ --------------------------------------
126
+
127
+ Normally, the array key of each item in the ``choices `` option is used as the
128
+ text that's shown to the user. But that can be completely customized via the
129
+ `choice_label `_ option. Check it out for more details.
130
+
131
+ .. _form-choices-simple-grouping :
132
+
133
+ Grouping Options
134
+ ----------------
135
+
136
+ You can easily "group" options in a select by passing a multi-dimensional choices array::
137
+
138
+ $builder->add('stockStatus', 'choice', [
139
+ 'choices' => [
140
+ 'Main Statuses' => [
141
+ 'Yes' => 'stock_yes',
142
+ 'No' => 'stock_no',
143
+ ],
144
+ 'Out of Stock Statuses' => [
145
+ 'Backordered' => 'stock_backordered',
146
+ 'Discontinued' => 'stock_discontinued',
147
+ ]
148
+ ],
149
+ 'choices_as_values' => true,
150
+ );
151
+
152
+ .. image :: /images/reference/form/choice-example3.png
153
+ :align: center
154
+ :scale: 50
155
+ :width: 350
156
+
157
+ To get fancier, use the `group_by `_ option.
158
+
104
159
Field Options
105
160
-------------
106
161
@@ -113,65 +168,92 @@ This is the most basic way to specify the choices that should be used
113
168
by this field. The ``choices `` option is an array, where the array key
114
169
is the item's label and the array value is the item's value::
115
170
116
- $builder->add('gender', 'choice', array(
117
- 'choices' => array('Male' => 'm', 'Female' => 'f'),
171
+ $builder->add('inStock', 'choice', array(
172
+ 'choices' => array('In Stock' => true, 'Out of Stock' => false),
173
+ // always include this
118
174
'choices_as_values' => true,
119
175
));
120
176
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
177
choices_as_values
127
178
~~~~~~~~~~~~~~~~~
128
179
129
180
**type **: ``boolean `` **default **: false
130
181
131
182
.. versionadded :: 2.7
132
183
133
- The ``choices_as_values `` option of ChoiceType was introduced in Symfony
134
- 2.7.
184
+ The ``choices_as_values `` option was introduced in Symfony 2.7.
135
185
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.
186
+ The ``choices_as_values `` option was added to keep backward compatibility with the
187
+ *old * way of handling the ``choices `` option. When set to ``false `` (or omitted),
188
+ the choice keys are used as the underlying value and the choice values are shown
189
+ to the user.
141
190
142
- * Before 2.7::
191
+ * Before 2.7 (and deprecated now) ::
143
192
144
193
$builder->add('gender', 'choice', array(
194
+ // Shows "Male" to the user, returns "m" when selected
145
195
'choices' => array('m' => 'Male', 'f' => 'Female'),
146
196
'choices_as_values' => false,
147
197
));
148
198
149
199
* Optional since 2.7::
150
200
151
201
$builder->add('gender', 'choice', array(
202
+ // Shows "Male" to the user, returns "m" when selected
152
203
'choices' => array('Male' => 'm', 'Female' => 'f'),
153
204
'choices_as_values' => true,
154
205
));
155
206
207
+ In Symfony 3.0, ``choices_as_values `` option will go away, but it's behavior will
208
+ always be used:
209
+
156
210
* Default for 3.0::
157
211
158
212
$builder->add('gender', 'choice', array(
159
213
'choices' => array('Male' => 'm', 'Female' => 'f'),
160
214
));
161
215
162
- .. caution ::
216
+ choice_loader
217
+ ~~~~~~~~~~~~~
218
+
219
+ .. versionadded :: 2.7
220
+
221
+ The ``choice_loader `` option was added in Symfony 2.7.
222
+
223
+ **type **: :class: `Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface `
224
+
225
+ The ``choice_loader `` can be used to only partially load the choices in cases where
226
+ a fully-loaded list is not necessary. This is only needed in advanced cases and
227
+ would replace the ``choices `` option.
228
+
229
+ .. _reference-form-choice-label :
230
+
231
+ .. include :: /reference/forms/types/options/choice_label.rst.inc
232
+
233
+ .. include :: /reference/forms/types/options/choice_attr.rst.inc
234
+
235
+ .. include :: /reference/forms/types/options/placeholder.rst.inc
236
+
237
+ .. include :: /reference/forms/types/options/expanded.rst.inc
238
+
239
+ .. include :: /reference/forms/types/options/multiple.rst.inc
240
+
241
+ .. include :: /reference/forms/types/options/preferred_choices.rst.inc
242
+
243
+ .. include :: /reference/forms/types/options/group_by.rst.inc
244
+
245
+ .. include :: /reference/forms/types/options/choice_value.rst.inc
246
+
247
+ .. include :: /reference/forms/types/options/choice_name.rst.inc
163
248
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.
167
249
168
250
choice_list
169
251
~~~~~~~~~~~
170
252
171
253
.. caution ::
172
254
173
255
The ``choice_list `` option of ChoiceType was deprecated in Symfony 2.7.
174
- You should use `` choices `` or `` choice_loader `` now.
256
+ You should use `choices `_ or `choice_loader `_ now.
175
257
176
258
**type **: :class: `Symfony\\ Component\\ Form\\ Extension\\ Core\\ ChoiceList\\ ChoiceListInterface `
177
259
@@ -207,37 +289,6 @@ But don't be confused! If ``Full`` is selected (value ``0`` in HTML), ``1``
207
289
will be returned in your form. If ``Almost empty `` is selected (value ``2 ``
208
290
in HTML), ``0.1 `` will be returned.
209
291
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
-
223
- .. include :: /reference/forms/types/options/placeholder.rst.inc
224
-
225
- .. include :: /reference/forms/types/options/expanded.rst.inc
226
-
227
- .. include :: /reference/forms/types/options/multiple.rst.inc
228
-
229
- .. include :: /reference/forms/types/options/preferred_choices.rst.inc
230
-
231
- .. include :: /reference/forms/types/options/choice_label.rst.inc
232
-
233
- .. include :: /reference/forms/types/options/choice_attr.rst.inc
234
-
235
- .. include :: /reference/forms/types/options/group_by.rst.inc
236
-
237
- .. include :: /reference/forms/types/options/choice_name.rst.inc
238
-
239
- .. include :: /reference/forms/types/options/choice_value.rst.inc
240
-
241
292
Overridden Options
242
293
------------------
243
294
0 commit comments