@@ -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,34 @@ 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 them in the sections below .
115
126
116
127
.. _forms-reference-choice-tags :
117
128
@@ -151,7 +162,7 @@ by passing a multi-dimensional ``choices`` array::
151
162
.. image :: /_images/reference/form/choice-example4.png
152
163
:align: center
153
164
154
- To get fancier, use the `group_by `_ option.
165
+ To get fancier, use the `group_by `_ option instead .
155
166
156
167
Field Options
157
168
-------------
@@ -169,7 +180,10 @@ is the item's label and the array value is the item's value::
169
180
// ...
170
181
171
182
$builder->add('inStock', ChoiceType::class, [
172
- 'choices' => ['In Stock' => true, 'Out of Stock' => false],
183
+ 'choices' => [
184
+ 'In Stock' => true,
185
+ 'Out of Stock' => false,
186
+ ],
173
187
]);
174
188
175
189
If there are choice values that are not scalar or the stringified
0 commit comments