@@ -52,10 +52,13 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
52
52
*/
53
53
public function buildForm (FormBuilderInterface $ builder , array $ options )
54
54
{
55
+ $ choiceList = $ this ->createChoiceList ($ options );
56
+ $ builder ->setAttribute ('choice_list ' , $ choiceList );
57
+
55
58
if ($ options ['expanded ' ]) {
56
59
$ builder ->setDataMapper ($ options ['multiple ' ]
57
- ? new CheckboxListMapper ($ options [ ' choice_list ' ] )
58
- : new RadioListMapper ($ options [ ' choice_list ' ] ));
60
+ ? new CheckboxListMapper ($ choiceList )
61
+ : new RadioListMapper ($ choiceList ));
59
62
60
63
// Initialize all choices before doing the index check below.
61
64
// This helps in cases where index checks are optimized for non
@@ -64,12 +67,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
64
67
// requires another SQL query. When the initialization is done first,
65
68
// one SQL query is sufficient.
66
69
67
- $ choiceListView = $ this ->createChoiceListView ($ options [ ' choice_list ' ] , $ options );
70
+ $ choiceListView = $ this ->createChoiceListView ($ choiceList , $ options );
68
71
$ builder ->setAttribute ('choice_list_view ' , $ choiceListView );
69
72
70
73
// Check if the choices already contain the empty value
71
74
// Only add the placeholder option if this is not the case
72
- if (null !== $ options ['placeholder ' ] && 0 === count ($ options [ ' choice_list ' ] ->getChoicesForValues (array ('' )))) {
75
+ if (null !== $ options ['placeholder ' ] && 0 === count ($ choiceList ->getChoicesForValues (array ('' )))) {
73
76
$ placeholderView = new ChoiceView (null , '' , $ options ['placeholder ' ]);
74
77
75
78
// "placeholder" is a reserved name
@@ -139,10 +142,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
139
142
}
140
143
} elseif ($ options ['multiple ' ]) {
141
144
// <select> tag with "multiple" option
142
- $ builder ->addViewTransformer (new ChoicesToValuesTransformer ($ options [ ' choice_list ' ] ));
145
+ $ builder ->addViewTransformer (new ChoicesToValuesTransformer ($ choiceList ));
143
146
} else {
144
147
// <select> tag without "multiple" option
145
- $ builder ->addViewTransformer (new ChoiceToValueTransformer ($ options [ ' choice_list ' ] ));
148
+ $ builder ->addViewTransformer (new ChoiceToValueTransformer ($ choiceList ));
146
149
}
147
150
148
151
if ($ options ['multiple ' ] && $ options ['by_reference ' ]) {
@@ -162,10 +165,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
162
165
$ choiceTranslationDomain = $ view ->vars ['translation_domain ' ];
163
166
}
164
167
168
+ /** @var ChoiceListInterface $choiceList */
169
+ $ choiceList = $ form ->getConfig ()->getAttribute ('choice_list ' );
170
+
165
171
/** @var ChoiceListView $choiceListView */
166
172
$ choiceListView = $ form ->getConfig ()->hasAttribute ('choice_list_view ' )
167
173
? $ form ->getConfig ()->getAttribute ('choice_list_view ' )
168
- : $ this ->createChoiceListView ($ options [ ' choice_list ' ] , $ options );
174
+ : $ this ->createChoiceListView ($ choiceList , $ options );
169
175
170
176
$ view ->vars = array_replace ($ view ->vars , array (
171
177
'multiple ' => $ options ['multiple ' ],
@@ -192,7 +198,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
192
198
}
193
199
194
200
// Check if the choices already contain the empty value
195
- $ view ->vars ['placeholder_in_choices ' ] = 0 !== count ($ options [ ' choice_list ' ] ->getChoicesForValues (array ('' )));
201
+ $ view ->vars ['placeholder_in_choices ' ] = 0 !== count ($ choiceList ->getChoicesForValues (array ('' )));
196
202
197
203
// Only add the empty value option if this is not the case
198
204
if (null !== $ options ['placeholder ' ] && !$ view ->vars ['placeholder_in_choices ' ]) {
@@ -235,8 +241,6 @@ public function finishView(FormView $view, FormInterface $form, array $options)
235
241
*/
236
242
public function configureOptions (OptionsResolver $ resolver )
237
243
{
238
- $ choiceListFactory = $ this ->choiceListFactory ;
239
-
240
244
$ emptyData = function (Options $ options ) {
241
245
if ($ options ['multiple ' ] || $ options ['expanded ' ]) {
242
246
return array ();
@@ -249,29 +253,20 @@ public function configureOptions(OptionsResolver $resolver)
249
253
return $ options ['required ' ] ? null : '' ;
250
254
};
251
255
252
- $ choiceListNormalizer = function (Options $ options ) use ($ choiceListFactory ) {
253
- if (null !== $ options ['choice_loader ' ]) {
254
- return $ choiceListFactory ->createListFromLoader (
255
- $ options ['choice_loader ' ],
256
- $ options ['choice_value ' ]
257
- );
256
+ $ choicesAsValuesNormalizer = function (Options $ options , $ choicesAsValues ) {
257
+ // Not set by the user
258
+ if (null === $ choicesAsValues ) {
259
+ return true ;
258
260
}
259
261
260
- // Harden against NULL values (like in EntityType and ModelType)
261
- $ choices = null !== $ options ['choices ' ] ? $ options ['choices ' ] : array ();
262
-
263
- return $ choiceListFactory ->createListFromChoices ($ choices , $ options ['choice_value ' ]);
264
- };
265
-
266
- $ choicesAsValuesNormalizer = function (Options $ options , $ choicesAsValues ) {
267
- if (null !== $ choicesAsValues ) {
268
- if (true !== $ choicesAsValues ) {
269
- throw new \RuntimeException ('The "choices_as_values" option should not be used. Remove it and flip the contents of the "choices" option instead. ' );
270
- }
271
- // To be uncommented in 3.1
272
- //@trigger_error('The "choices_as_values" option is deprecated since version 3.1 and will be removed in 4.0. You should not use it anymore.', E_USER_DEPRECATED);
262
+ // Set by the user
263
+ if (true !== $ choicesAsValues ) {
264
+ throw new \RuntimeException ('The "choices_as_values" option should not be used. Remove it and flip the contents of the "choices" option instead. ' );
273
265
}
274
266
267
+ // To be uncommented in 3.1
268
+ //@trigger_error('The "choices_as_values" option is deprecated since version 3.1 and will be removed in 4.0. You should not use it anymore.', E_USER_DEPRECATED);
269
+
275
270
return true ;
276
271
};
277
272
@@ -306,9 +301,8 @@ public function configureOptions(OptionsResolver $resolver)
306
301
$ resolver ->setDefaults (array (
307
302
'multiple ' => false ,
308
303
'expanded ' => false ,
309
- 'choice_list ' => null , // deprecated
310
304
'choices ' => array (),
311
- 'choices_as_values ' => null , // to be deprecated in 3.1
305
+ 'choices_as_values ' => null , // to be deprecated in 3.1
312
306
'choice_loader ' => null ,
313
307
'choice_label ' => null ,
314
308
'choice_name ' => null ,
@@ -327,12 +321,10 @@ public function configureOptions(OptionsResolver $resolver)
327
321
'choice_translation_domain ' => true ,
328
322
));
329
323
330
- $ resolver ->setNormalizer ('choice_list ' , $ choiceListNormalizer );
331
324
$ resolver ->setNormalizer ('placeholder ' , $ placeholderNormalizer );
332
325
$ resolver ->setNormalizer ('choice_translation_domain ' , $ choiceTranslationDomainNormalizer );
333
326
$ resolver ->setNormalizer ('choices_as_values ' , $ choicesAsValuesNormalizer );
334
327
335
- $ resolver ->setAllowedTypes ('choice_list ' , array ('null ' , 'Symfony\Component\Form\ChoiceList\ChoiceListInterface ' ));
336
328
$ resolver ->setAllowedTypes ('choices ' , array ('null ' , 'array ' , '\Traversable ' ));
337
329
$ resolver ->setAllowedTypes ('choice_translation_domain ' , array ('null ' , 'bool ' , 'string ' ));
338
330
$ resolver ->setAllowedTypes ('choice_loader ' , array ('null ' , 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface ' ));
@@ -422,6 +414,21 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch
422
414
$ builder ->add ($ name , $ choiceType , $ choiceOpts );
423
415
}
424
416
417
+ private function createChoiceList (array $ options )
418
+ {
419
+ if (null !== $ options ['choice_loader ' ]) {
420
+ return $ this ->choiceListFactory ->createListFromLoader (
421
+ $ options ['choice_loader ' ],
422
+ $ options ['choice_value ' ]
423
+ );
424
+ }
425
+
426
+ // Harden against NULL values (like in EntityType and ModelType)
427
+ $ choices = null !== $ options ['choices ' ] ? $ options ['choices ' ] : array ();
428
+
429
+ return $ this ->choiceListFactory ->createListFromChoices ($ choices , $ options ['choice_value ' ]);
430
+ }
431
+
425
432
private function createChoiceListView (ChoiceListInterface $ choiceList , array $ options )
426
433
{
427
434
// If no explicit grouping information is given, use the structural
0 commit comments