Skip to content

Commit 54ebb94

Browse files
bug #17163 [Form] fix Catchable Fatal Error if choices is not an array (Gladhon, nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] fix Catchable Fatal Error if choices is not an array | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | Since 2.7.8 I got a BC-Break Error Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\Extension\Core\Type\ChoiceType::normalizeLegacyChoices() must be of the type array, null given normalizeLegacyChoices work only with array, so if choices not an array, just don't try to normlize. Commits ------- f3c2a9b [Form] fix Catchable Fatal Error if choices is not an array
2 parents 9df35a9 + 974fddb commit 54ebb94

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

525529
$choiceLabels->labels[$nextKey] = $choiceLabel;
526-
$choices[$choice] = $nextKey++;
530+
$normalizedChoices[$choice] = $nextKey++;
527531
}
532+
533+
return $normalizedChoices;
528534
}
529535
}

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)