Skip to content

Commit 974fddb

Browse files
Gladhonnicolas-grekas
authored andcommitted
[Form] fix Catchable Fatal Error if choices is not an array
1 parent 3d95d1c commit 974fddb

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

Extension/Core/Type/ChoiceType.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,11 @@ public function configureOptions(OptionsResolver $resolver)
263263
return $choices;
264264
}
265265

266-
ChoiceType::normalizeLegacyChoices($choices, $choiceLabels);
266+
if (null === $choices) {
267+
return;
268+
}
267269

268-
return $choices;
270+
return ChoiceType::normalizeLegacyChoices($choices, $choiceLabels);
269271
};
270272

271273
// BC closure, to be removed in 3.0
@@ -503,26 +505,30 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
503505
* are lost. Store them in a utility array that is used from the
504506
* "choice_label" closure by default.
505507
*
506-
* @param array $choices The choice labels indexed by choices.
507-
* Labels are replaced by generated keys.
508-
* @param object $choiceLabels The object that receives the choice labels
509-
* indexed by generated keys.
510-
* @param int $nextKey The next generated key.
508+
* @param array|\Traversable $choices The choice labels indexed by choices.
509+
* @param object $choiceLabels The object that receives the choice labels
510+
* indexed by generated keys.
511+
* @param int $nextKey The next generated key.
512+
*
513+
* @return array The choices in a normalized array with labels replaced by generated keys.
511514
*
512515
* @internal Public only to be accessible from closures on PHP 5.3. Don't
513516
* use this method as it may be removed without notice and will be in 3.0.
514517
*/
515-
public static function normalizeLegacyChoices(array &$choices, $choiceLabels, &$nextKey = 0)
518+
public static function normalizeLegacyChoices($choices, $choiceLabels, &$nextKey = 0)
516519
{
520+
$normalizedChoices = array();
521+
517522
foreach ($choices as $choice => $choiceLabel) {
518-
if (is_array($choiceLabel)) {
519-
$choiceLabel = ''; // Dereference $choices[$choice]
520-
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
523+
if (is_array($choiceLabel) || $choiceLabel instanceof \Traversable) {
524+
$normalizedChoices[$choice] = self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey);
521525
continue;
522526
}
523527

524528
$choiceLabels->labels[$nextKey] = $choiceLabel;
525-
$choices[$choice] = $nextKey++;
529+
$normalizedChoices[$choice] = $nextKey++;
526530
}
531+
532+
return $normalizedChoices;
527533
}
528534
}

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,21 @@ public function testSubmitSingleNonExpandedObjectChoices()
487487
$this->assertTrue($form->isSynchronized());
488488
}
489489

490+
/**
491+
* @group legacy
492+
*/
493+
public function testLegacyNullChoices()
494+
{
495+
$form = $this->factory->create('choice', null, array(
496+
'multiple' => false,
497+
'expanded' => false,
498+
'choices' => null,
499+
));
500+
$this->assertNull($form->getConfig()->getOption('choices'));
501+
$this->assertFalse($form->getConfig()->getOption('multiple'));
502+
$this->assertFalse($form->getConfig()->getOption('expanded'));
503+
}
504+
490505
/**
491506
* @group legacy
492507
*/

0 commit comments

Comments
 (0)