8000 [Form] Fix choices defined as Traversable · symfony/symfony@021d93a · GitHub
[go: up one dir, main page]

Skip to content

Commit 021d93a

Browse files
[Form] Fix choices defined as Traversable
1 parent bc642fb commit 021d93a

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/Symfony/Component/Form/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
@@ -520,26 +522,30 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
520522
* are lost. Store them in a utility array that is used from the
521523
* "choice_label" closure by default.
522524
*
523-
* @param array $choices The choice labels indexed by choices.
524-
* Labels are replaced by generated keys.
525-
* @param object $choiceLabels The object that receives the choice labels
526-
* indexed by generated keys.
527-
* @param int $nextKey The next generated key.
525+
* @param array|\Traversable $choices The choice labels indexed by choices.
526+
* @param object $choiceLabels The object that receives the choice labels
527+
* indexed by generated keys.
528+
* @param int $nextKey The next generated key.
529+
*
530+
* @return array The choices in a normalized array with labels replaced by generated keys.
528531
*
529532
* @internal Public only to be accessible from closures on PHP 5.3. Don't
530533
* use this method as it may be removed without notice and will be in 3.0.
531534
*/
532-
public static function normalizeLegacyChoices(array &$choices, $choiceLabels, &$nextKey = 0)
535+
public static function normalizeLegacyChoices($choices, $choiceLabels, &$nextKey = 0)
533536
{
537+
$normalizedChoices = array();
538+
534539
foreach ($choices as $choice => $choiceLabel) {
535-
if (is_array($choiceLabel)) {
536-
$choiceLabel = ''; // Dereference $choices[$choice]
537-
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
540+
if (is_array($choiceLabel) || $choiceLabel instanceof \Traversable) {
541+
$normalizedChoices[$choice] = self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey);
538542
continue;
539543
}
540544

541545
$choiceLabels->labels[$nextKey] = $choiceLabel;
542-
$choices[$choice] = $nextKey++;
546+
$normalizedChoices[$choice] = $nextKey++;
543547
}
548+
549+
return $normalizedChoices;
544550
}
545551
}

0 commit comments

Comments
 (0)
0