32
32
use Symfony \Component \Form \Extension \Core \EventListener \MergeCollectionListener ;
33
33
use Symfony \Component \Form \Extension \Core \DataTransformer \ChoiceToValueTransformer ;
34
34
use Symfony \Component \Form \Extension \Core \DataTransformer \ChoicesToValuesTransformer ;
35
+ use Symfony \Component \Form \Extension \Core \DataTransformer \ValuesToStringTransformer ;
35
36
use Symfony \Component \OptionsResolver \Options ;
36
37
use Symfony \Component \OptionsResolver \OptionsResolver ;
37
38
@@ -54,7 +55,7 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
54
55
*/
55
56
public function buildForm (FormBuilderInterface $ builder , array $ options )
56
57
{
57
- if ($ options ['expanded ' ]) {
58
+ if (' expanded ' == $ options ['widget ' ]) {
58
59
$ builder ->setDataMapper ($ options ['multiple ' ]
59
60
? new CheckboxListMapper ($ options ['choice_list ' ])
60
61
: new RadioListMapper ($ options ['choice_list ' ]));
@@ -140,10 +141,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
140
141
});
141
142
}
142
143
} elseif ($ options ['multiple ' ]) {
143
- // < select> tag with "multiple" option
144
+ // " select", "text" or "hidden" widget with "multiple" option
144
145
$ builder ->addViewTransformer (new ChoicesToValuesTransformer ($ options ['choice_list ' ]));
146
+
147
+ // for "text" / "hidden" widget, view data uses a delimiter
148
+ if (in_array ($ options ['widget ' ], array ('text ' , 'hidden ' ))) {
149
+ $ builder ->addViewTransformer (new ValuesToStringTransformer ($ options ['delimiter ' ], $ options ['trim ' ]));
150
+ }
145
151
} else {
146
- // < select> tag without "multiple" option
152
+ // " select", "text" or "hidden" tag without "multiple" option
147
153
$ builder ->addViewTransformer (new ChoiceToValueTransformer ($ options ['choice_list ' ]));
148
154
}
149
155
@@ -170,8 +176,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
170
176
: $ this ->createChoiceListView ($ options ['choice_list ' ], $ options );
171
177
172
178
$ view ->vars = array_replace ($ view ->vars , array (
179
+ 'widget ' => $ options ['widget ' ],
173
180
'multiple ' => $ options ['multiple ' ],
174
- 'expanded ' => $ options ['expanded ' ],
181
+ 'expanded ' => $ options ['expanded ' ], // BC
175
182
'preferred_choices ' => $ choiceListView ->preferredChoices ,
176
183
'choices ' => $ choiceListView ->choices ,
177
184
'separator ' => '------------------- ' ,
@@ -205,7 +212,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
205
212
$ view ->vars ['empty_value ' ] = $ view ->vars ['placeholder ' ];
206
213
$ view ->vars ['empty_value_in_choices ' ] = $ view ->vars ['placeholder_in_choices ' ];
207
214
208
- if ($ options ['multiple ' ] && ! $ options ['expanded ' ]) {
215
+ if (' select ' == $ options ['widget ' ] && $ options ['multiple ' ]) {
209
216
// Add "[]" to the name in case a select tag with multiple options is
210
217
// displayed. Otherwise only one of the selected options is sent in the
211
218
// POST request.
@@ -218,7 +225,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
218
225
*/
219
226
public function finishView (FormView $ view , FormInterface $ form , array $ options )
220
227
{
221
- if ($ options ['expanded ' ]) {
228
+ if (' expanded ' == $ options ['widget ' ]) {
222
229
// Radio buttons should have the same name as the parent
223
230
$ childName = $ view ->vars ['full_name ' ];
224
231
@@ -240,6 +247,10 @@ public function configureOptions(OptionsResolver $resolver)
240
247
{
241
248
$ choiceListFactory = $ this ->choiceListFactory ;
242
249
250
+ $ widget = function (Options $ options ) {
251
+ return $ options ['expanded ' ] ? 'expanded ' : 'select ' ;
252
+ };
253
+
243
254
$ emptyData = function (Options $ options ) {
244
255
if ($ options ['multiple ' ] || $ options ['expanded ' ]) {
245
256
return array ();
@@ -303,7 +314,7 @@ public function configureOptions(OptionsResolver $resolver)
303
314
};
304
315
305
316
$ compound = function (Options $ options ) {
306
- return $ options ['expanded ' ];
317
+ return ' expanded ' == $ options ['widget ' ];
307
318
};
308
319
309
320
$ choiceTranslationDomainNormalizer = function (Options $ options , $ choiceTranslationDomain ) {
@@ -315,8 +326,10 @@ public function configureOptions(OptionsResolver $resolver)
315
326
};
316
327
317
328
$ resolver ->setDefaults (array (
329
+ 'widget ' => $ widget ,
318
330
'multiple ' => false ,
319
- 'expanded ' => false ,
331
+ 'delimiter ' => ', ' ,
332
+ 'expanded ' => false , // deprecated
320
333
'choice_list ' => null , // deprecated
321
334
'choices ' => array (),
322
335
'choices_as_values ' => false ,
0 commit comments