Skip to content

Commit 7aa4106

Browse files
committed
Completely re-working all the choice docs, including screenshots - for 2.7 changes
1 parent 79c905c commit 7aa4106

14 files changed

+303
-227
lines changed
28.3 KB
Loading
29.8 KB
Loading
33 KB
Loading
50.5 KB
Loading
39.2 KB
Loading

reference/forms/types/choice.rst

Lines changed: 146 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
.. index::
22
single: Forms; Fields; choice
33

4-
choice Field Type
5-
=================
4+
choice Field Type (select drop-downs, radio buttons & checkboxes)
5+
=================================================================
66

77
A multi-purpose field used to allow the user to "choose" one or more options.
88
It can be rendered as a ``select`` tag, radio buttons, or checkboxes.
99

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.
1211

1312
+-------------+------------------------------------------------------------------------------+
1413
| Rendered as | can be various tags (see below) |
1514
+-------------+------------------------------------------------------------------------------+
1615
| Options | - `choices`_ |
17-
| | - `choice_list`_ |
16+
| | - `choices_as_values`_ |
1817
| | - `choice_loader`_ |
1918
| | - `choice_label`_ |
2019
| | - `choice_attr`_ |
21-
| | - `choices_as_values`_ |
2220
| | - `placeholder`_ |
2321
| | - `expanded`_ |
2422
| | - `multiple`_ |
2523
| | - `preferred_choices`_ |
2624
| | - `group_by`_ |
27-
| | - `choice_name`_ |
2825
| | - `choice_value`_ |
26+
| | - `choice_name`_ |
27+
| | - `choice_list`_ (deprecated) |
2928
+-------------+------------------------------------------------------------------------------+
3029
| Overridden | - `compound`_ |
3130
| options | - `empty_data`_ |
@@ -51,56 +50,112 @@ Example Usage
5150
-------------
5251

5352
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::
5954

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*
6262
'choices_as_values' => true,
63-
'required' => false,
6463
));
6564

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:
6966

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
7971

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.
8376

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::
9678

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.
99119

100120
.. _forms-reference-choice-tags:
101121

102122
.. include:: /reference/forms/types/options/select_how_rendered.rst.inc
103123

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+
104159
Field Options
105160
-------------
106161

@@ -113,65 +168,92 @@ This is the most basic way to specify the choices that should be used
113168
by this field. The ``choices`` option is an array, where the array key
114169
is the item's label and the array value is the item's value::
115170

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
118174
'choices_as_values' => true,
119175
));
120176

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-
126177
choices_as_values
127178
~~~~~~~~~~~~~~~~~
128179

129180
**type**: ``boolean`` **default**: false
130181

131182
.. versionadded:: 2.7
132183

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.
135185

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.
141190

142-
* Before 2.7::
191+
* Before 2.7 (and deprecated now)::
143192

144193
$builder->add('gender', 'choice', array(
194+
// Shows "Male" to the user, returns "m" when selected
145195
'choices' => array('m' => 'Male', 'f' => 'Female'),
146196
'choices_as_values' => false,
147197
));
148198

149199
* Optional since 2.7::
150200

151201
$builder->add('gender', 'choice', array(
202+
// Shows "Male" to the user, returns "m" when selected
152203
'choices' => array('Male' => 'm', 'Female' => 'f'),
153204
'choices_as_values' => true,
154205
));
155206

207+
In Symfony 3.0, ``choices_as_values`` option will go away, but it's behavior will
208+
always be used:
209+
156210
* Default for 3.0::
157211

158212
$builder->add('gender', 'choice', array(
159213
'choices' => array('Male' => 'm', 'Female' => 'f'),
160214
));
161215

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
163248

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.
167249

168250
choice_list
169251
~~~~~~~~~~~
170252

171253
.. caution::
172254

173255
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.
175257

176258
**type**: :class:`Symfony\\Component\\Form\\Extension\\Core\\ChoiceList\\ChoiceListInterface`
177259

@@ -207,37 +289,6 @@ But don't be confused! If ``Full`` is selected (value ``0`` in HTML), ``1``
207289
will be returned in your form. If ``Almost empty`` is selected (value ``2``
208290
in HTML), ``0.1`` will be returned.
209291

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-
241292
Overridden Options
242293
------------------
243294

0 commit comments

Comments
 (0)