@@ -74,8 +74,8 @@ This will create a ``select`` drop-down like this:
74
74
75
75
If the user selects ``No ``, the form will return ``false `` for this field. Similarly,
76
76
if the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
77
- In other words, the **value ** of each item is the value you want to get/set in PHP
78
- code, while the **key ** is what will be shown to the user.
77
+ In other words, the **choice ** of each item is the value you want to get/set in PHP
78
+ code, while the **key ** is the ** label ** that will be shown to the user.
79
79
80
80
Advanced Example (with Objects!)
81
81
--------------------------------
@@ -95,23 +95,40 @@ method::
95
95
new Category('Cat3'),
96
96
new Category('Cat4'),
97
97
],
98
- 'choice_label' => function(Category $category, $key, $value) {
99
- return strtoupper($category->getName());
98
+ // "name" is a property path, meaning Symfony will look for a public
99
+ // property or a public method like "getName()" to define the input
100
+ // string value that will be submitted by the form
101
+ 'choice_value' => 'name',
102
+ // a callback to return the label for a given choice
103
+ // if a placeholder is used, its empty value (null) may be passed but
104
+ // its label is defined by its own "placeholder" option
105
+ 'choice_label' => function(?Category $category) {
106
+ return $category ? strtoupper($category->getName()) : '';
100
107
},
101
- 'choice_attr' => function(Category $category, $key, $value) {
102
- return ['class' => 'category_'.strtolower($category->getName())];
108
+ // returns the html attributes for each option input (may be radio/checkbox)
109
+ 'choice_attr' => function(?Category $category) {
110
+ return $category ? ['class' => 'category_'.strtolower($category->getName())] : [];
103
111
},
104
- 'group_by' => function(Category $category, $key, $value) {
112
+ // every option can use a string property path or any callable that get
113
+ // passed each choice as argument, but it may not be needed
114
+ 'group_by' => function() {
105
115
// randomly assign things into 2 groups
106
116
return rand(0, 1) == 1 ? 'Group A' : 'Group B';
107
117
},
108
- 'preferred_choices' => function(Category $category, $key, $value) {
109
- return $category->getName() == 'Cat2' || $category->getName() == 'Cat3';
118
+ // a callback to return whether a category is preferred
119
+ 'preferred_choices' => function(?Category $category) {
120
+ return $category && 100 < $category->getArticleCounts();
110
121
},
111
122
]);
112
123
113
- You can also customize the `choice_name `_ and `choice_value `_ of each choice if
114
- you need further HTML customization.
124
+ You can also customize the `choice_name `_ of each choice. You can learn more
125
+ about all of these options in the sections below.
126
+
127
+ .. caution ::
128
+
129
+ The *placeholder * is a specific field, when the choices are optional the
130
+ first item in the list must be empty, so the user can unselect.
131
+ Be sure to always handle the empty choice ``null `` when using callbacks.
115
132
116
133
.. _forms-reference-choice-tags :
117
134
@@ -151,7 +168,7 @@ by passing a multi-dimensional ``choices`` array::
151
168
.. image :: /_images/reference/form/choice-example4.png
152
169
:align: center
153
170
154
- To get fancier, use the `group_by `_ option.
171
+ To get fancier, use the `group_by `_ option instead .
155
172
156
173
Field Options
157
174
-------------
@@ -169,7 +186,10 @@ is the item's label and the array value is the item's value::
169
186
// ...
170
187
171
188
$builder->add('inStock', ChoiceType::class, [
172
- 'choices' => ['In Stock' => true, 'Out of Stock' => false],
189
+ 'choices' => [
190
+ 'In Stock' => true,
191
+ 'Out of Stock' => false,
192
+ ],
173
193
]);
174
194
175
195
If there are choice values that are not scalar or the stringified
0 commit comments