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

Skip to content

Commit 6b47cd5

Browse files
[Form] Fix choices defined as Traversable
1 parent bc642fb commit 6b47cd5

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

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

266-
ChoiceType::normalizeLegacyChoices($choices, $choiceLabels);
267-
268-
return $choices;
266+
return ChoiceType::normalizeLegacyChoices($choices, $choiceLabels);
269267
};
270268

271269
// BC closure, to be removed in 3.0
@@ -520,26 +518,30 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
520518
* are lost. Store them in a utility array that is used from the
521519
* "choice_label" closure by default.
522520
*
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.
521+
* @param array|\Traversable $choices The choice labels indexed by choices.
522+
* @param object $choiceLabels The object that receives the choice labels
523+
* indexed by generated keys.
524+
* @param int $nextKey The next generated key.
525+
*
526+
* @return array The choices in a normalized array with labels replaced by generated keys.
528527
*
529528
* @internal Public only to be accessible from closures on PHP 5.3. Don't
530529
* use this method as it may be removed without notice and will be in 3.0.
531530
*/
532-
public static function normalizeLegacyChoices(array &$choices, $choiceLabels, &$nextKey = 0)
531+
public static function normalizeLegacyChoices($choices, $choiceLabels, &$nextKey = 0)
533532
{
533+
$normalizedChoices = array();
534+
534535
foreach ($choices as $choice => $choiceLabel) {
535-
if (is_array($choiceLabel)) {
536-
$choiceLabel = ''; // Dereference $choices[$choice]
537-
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
536+
if (is_array($choiceLabel) || $choiceLabel instanceof \Traversable) {
537+
$normalizedChoices[$choice] = self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey);
538538
continue;
539539
}
540540

541541
$choiceLabels->labels[$nextKey] = $choiceLabel;
542-
$choices[$choice] = $nextKey++;
542+
$normalizedChoices[$choice] = $nextKey++;
543543
}
544+
545+
return $normalizedChoices;
544546
}
545547
}

0 commit comments

Comments
 (0)
0