@@ -70,10 +70,21 @@ This will create a ``select`` drop-down like this:
70
70
.. image :: /images/reference/form/choice-example1.png
71
71
:align: center
72
72
73
- If the user selects ``No ``, the form will return ``false `` for this field. Similarly,
74
- if the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
75
- In other words, the **value ** of each item is the value you want to get/set in PHP
76
- code, while the **key ** is what will be shown to the user.
73
+ The model data of this field, the **choice ** may be any of the ``choices `` option
74
+ values, while **keys ** are used as default label that the user will see and select.
75
+ If the user selects ``No ``, the form will return a string representation of that
76
+ choice, a string **value ** (html attribute) which is by default the **choice ** data
77
+ casted to string.
78
+ Hence, when choices are objects, either they should implement ``__toString() ``
79
+ method or the option ``choice_value `` should be defined.
80
+ When the **choices ** cannot be casted to string or when ``choice_value `` returns
81
+ either ``null `` or non unique values an incrementing integer is used as **value **.
82
+
83
+ If the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
84
+ In other words, each value of the ``choices `` option is the **choice ** data you
85
+ want to deal with in PHP code, while the **key ** is the default label that will be
86
+ shown to the user and the **value ** is the string that will be submitted to the
87
+ form and used in the template for the corresponding html attribute.
77
88
78
89
.. caution ::
79
90
@@ -83,40 +94,68 @@ code, while the **key** is what will be shown to the user.
83
94
and will be removed in 3.0. To read about the old API, read an older version of
84
95
the docs.
85
96
97
+ .. note ::
98
+
99
+ Pre selected choices will depend on the **data ** passed to field and the values of
100
+ the ``choices `` option. However submitted choices will depend on the **string **
101
+ matching the **choice **. In the example above, the default values falls back on an
102
+ incrementing integer because ``null `` cannot be casted to string.
103
+ You should consider it as well when dealing with ``emtpy_data `` option::
104
+
105
+ $builder->add('isAttending', 'choice', array(
106
+ 'choices' => array(
107
+ 'Maybe' => null,
108
+ 'Yes' => true,
109
+ 'No' => false,
110
+ ),
111
+ 'choices_as_values' => true,
112
+ 'data' => true, // pre selected choice
113
+ 'empty_data' => '1', // default submitted value
114
+ ));
115
+
116
+ Also note that as a ``scalar ``, ``false `` string **value ** is by default ``"0" ``
117
+ to avoid conflict with placeholder value which is always an empty string.
118
+
86
119
Advanced Example (with Objects!)
87
120
--------------------------------
88
121
89
122
This field has a *lot * of options and most control how the field is displayed. In
90
123
this example, the underlying data is some ``Category `` object that has a ``getName() ``
91
124
method::
92
125
93
- $builder->add('category', 'choice', [
94
- 'choices' => [
126
+ $builder->add('category', 'choice', array(
127
+ 'choices' => array(
95
128
new Category('Cat1'),
96
129
new Category('Cat2'),
97
130
new Category('Cat3'),
98
131
new Category('Cat4'),
99
- ] ,
132
+ ) ,
100
133
'choices_as_values' => true,
101
- 'choice_label' => function($category, $key, $index ) {
134
+ 'choice_label' => function($category, $key, $value ) {
102
135
/** @var Category $category */
103
136
return strtoupper($category->getName());
104
137
},
105
- 'choice_attr' => function($category, $key, $index ) {
106
- return [ 'class' => 'category_'.strtolower($category->getName())] ;
138
+ 'choice_attr' => function($category, $key, $value ) {
139
+ return array( 'class' => 'category_'.strtolower($category->getName())) ;
107
140
},
108
- 'group_by' => function($category, $key, $index ) {
141
+ 'group_by' => function($category, $key, $value ) {
109
142
// randomly assign things into 2 groups
110
143
return rand(0, 1) == 1 ? 'Group A' : 'Group B'
111
144
},
112
- 'preferred_choices' => function($category, $key, $index ) {
145
+ 'preferred_choices' => function($category, $key, $value ) {
113
146
return $category->getName() == 'Cat2' || $category->getName() == 'Cat3';
114
147
},
115
- ] );
148
+ ) );
116
149
117
150
You can also customize the `choice_name `_ and `choice_value `_ of each choice if
118
151
you need further HTML customization.
119
152
153
+ .. caution ::
154
+
155
+ We avoid type hinting ``Category `` in this example as it may conflict
156
+ if the placeholder is passed, also you should test whether ``$choice ``
157
+ is ``null `` before calling any method or object property.
158
+
120
159
.. _forms-reference-choice-tags :
121
160
122
161
.. include :: /reference/forms/types/options/select_how_rendered.rst.inc
@@ -135,19 +174,19 @@ Grouping Options
135
174
136
175
You can easily "group" options in a select by passing a multi-dimensional choices array::
137
176
138
- $builder->add('stockStatus', 'choice', [
139
- 'choices' => [
140
- 'Main Statuses' => [
177
+ $builder->add('stockStatus', 'choice', array(
178
+ 'choices' => array(
179
+ 'Main Statuses' => array(
141
180
'Yes' => 'stock_yes',
142
181
'No' => 'stock_no',
143
- ] ,
144
- 'Out of Stock Statuses' => [
182
+ ) ,
183
+ 'Out of Stock Statuses' => array(
145
184
'Backordered' => 'stock_backordered',
146
185
'Discontinued' => 'stock_discontinued',
147
- ]
148
- ] ,
186
+ ),
187
+ ) ,
149
188
'choices_as_values' => true,
150
- );
189
+ )) ;
151
190
152
191
.. image :: /images/reference/form/choice-example4.png
153
192
:align: center
@@ -160,18 +199,24 @@ Field Options
160
199
choices
161
200
~~~~~~~
162
201
163
- **type **: ``array `` **default **: ``array() ``
202
+ **type **: ``array `` or `` \Traversable `` **default **: ``array() ``
164
203
165
204
This is the most basic way to specify the choices that should be used
166
205
by this field. The ``choices `` option is an array, where the array key
167
- is the item 's label and the array value is the item 's value ::
206
+ is the choice 's label and the array value is the choice 's data ::
168
207
169
208
$builder->add('inStock', 'choice', array(
170
- 'choices' => array('In Stock' => true, 'Out of Stock' => false),
209
+ 'choices' => array(
210
+ 'In Stock' => true,
211
+ 'Out of Stock' => false,
212
+ ),
171
213
// always include this
172
214
'choices_as_values' => true,
173
215
));
174
216
217
+ The component will try to cast the choices data to string to use it in view
218
+ format, but you can customize it using the `choice_value `_ option.
219
+
175
220
.. include :: /reference/forms/types/options/choice_attr.rst.inc
176
221
177
222
.. _reference-form-choice-label :
@@ -229,9 +274,14 @@ choice_loader
229
274
230
275
**type **: :class: `Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface `
231
276
232
- The ``choice_loader `` can be used to only partially load the choices in cases where
233
- a fully-loaded list is not necessary. This is only needed in advanced cases and
234
- would replace the ``choices `` option.
277
+ The ``choice_loader `` can be used to load the choices form a data source with a
278
+ custom logic (e.g query language) such as database or search engine.
279
+ The list will be fully loaded to display the form, but while submission only the
280
+ submitted choices will be loaded.
281
+
282
+ Also, the :class: ``Symfony\\ Component\\ Form\\ ChoiceList\\ Factory\\ ChoiceListFactoryInterface` ` will cache the choice list
283
+ so the same :class: ``Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface` ` can be used in different fields with more performance
284
+ (reducing N queries to 1).
235
285
236
286
.. include :: /reference/forms/types/options/choice_name.rst.inc
237
287
@@ -250,8 +300,8 @@ choices_as_values
250
300
251
301
The ``choices_as_values `` option was added to keep backward compatibility with the
252
302
*old * way of handling the ``choices `` option. When set to ``false `` (or omitted),
253
- the choice keys are used as the underlying value and the choice values are shown
254
- to the user.
303
+ the choice keys are used as the view value and the choice values are shown
304
+ to the user as label .
255
305
256
306
* Before 2.7 (and deprecated now)::
257
307
@@ -271,12 +321,11 @@ to the user.
271
321
'choices_as_values' => true,
272
322
));
273
323
274
- In Symfony 3.0, the ``choices_as_values `` option doesn't exist, but the ``choice ``
275
- type behaves as if it were set to true:
324
+ As of Symfony 3.0, the ``choices_as_values `` option is ``true `` by default:
276
325
277
326
* Default for 3.0::
278
327
279
- $builder->add('gender', 'choice ', array(
328
+ $builder->add('gender', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType ', array(
280
329
'choices' => array('Male' => 'm', 'Female' => 'f'),
281
330
));
282
331
0 commit comments