@@ -82,8 +82,8 @@ This will create a ``select`` drop-down like this:
82
82
83
83
If the user selects ``No ``, the form will return ``false `` for this field. Similarly,
84
84
if the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
85
- In other words, the **value ** of each item is the value you want to get/set in PHP
86
- code, while the **key ** is what will be shown to the user.
85
+ In other words, the **choice ** of each item is the value you want to get/set in PHP
86
+ code, while the **key ** is the ** label ** that will be shown to the user.
87
87
88
88
Advanced Example (with Objects!)
89
89
--------------------------------
@@ -103,23 +103,40 @@ method::
103
103
new Category('Cat3'),
104
104
new Category('Cat4'),
105
105
],
106
- 'choice_label' => function(Category $category, $key, $value) {
107
- return strtoupper($category->getName());
106
+ // "name" is a property path, meaning Symfony will look for a public
107
+ // property or a public method like "getName()" to define the input
108
+ // string value that will be submitted by the form
109
+ 'choice_value' => 'name',
110
+ // a callback to return the label for a given choice
111
+ // if a placeholder is used, its empty value (null) may be passed but
112
+ // its label is defined by its own "placeholder" option
113
+ 'choice_label' => function(?Category $category) {
114
+ return $category ? strtoupper($category->getName()) : '';
108
115
},
109
- 'choice_attr' => function(Category $category, $key, $value) {
110
- return ['class' => 'category_'.strtolower($category->getName())];
116
+ // returns the html attributes for each option input (may be radio/checkbox)
117
+ 'choice_attr' => function(?Category $category) {
118
+ return $category ? ['class' => 'category_'.strtolower($category->getName())] : [];
111
119
},
112
- 'group_by' => function(Category $category, $key, $value) {
120
+ // every option can use a string property path or any callable that get
121
+ // passed each choice as argument, but it may not be needed
122
+ 'group_by' => function() {
113
123
// randomly assign things into 2 groups
114
124
return rand(0, 1) == 1 ? 'Group A' : 'Group B';
115
125
},
116
- 'preferred_choices' => function(Category $category, $key, $value) {
117
- return $category->getName() == 'Cat2' || $category->getName() == 'Cat3';
126
+ // a callback to return whether a category is preferred
127
+ 'preferred_choices' => function(?Category $category) {
128
+ return $category && 100 < $category->getArticleCounts();
118
129
},
119
130
]);
120
131
121
- You can also customize the `choice_name `_ and `choice_value `_ of each choice if
122
- you need further HTML customization.
132
+ You can also customize the `choice_name `_ of each choice. You can learn more
133
+ about all of these options in the sections below.
134
+
135
+ .. caution ::
136
+
137
+ The *placeholder * is a specific field, when the choices are optional the
138
+ first item in the list must be empty, so the user can unselect.
139
+ Be sure to always handle the empty choice ``null `` when using callbacks.
123
140
124
141
.. _forms-reference-choice-tags :
125
142
@@ -159,7 +176,7 @@ by passing a multi-dimensional ``choices`` array::
159
176
.. image :: /_images/reference/form/choice-example4.png
160
177
:align: center
161
178
162
- To get fancier, use the `group_by `_ option.
179
+ To get fancier, use the `group_by `_ option instead .
163
180
164
181
Field Options
165
182
-------------
@@ -177,7 +194,10 @@ is the item's label and the array value is the item's value::
177
194
// ...
178
195
179
196
$builder->add('inStock', ChoiceType::class, [
180
- 'choices' => ['In Stock' => true, 'Out of Stock' => false],
197
+ 'choices' => [
198
+ 'In Stock' => true,
199
+ 'Out of Stock' => false,
200
+ ],
181
201
]);
182
202
183
203
If there are choice values that are not scalar or the stringified
0 commit comments