Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8a1352

Browse filesBrowse files
committed
bug #16796 [Form] Fix choices defined as Traversable (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [Form] Fix choices defined as Traversable | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16792 | License | MIT | Doc PR | - Commits ------- 021d93a [Form] Fix choices defined as Traversable
2 parents 54c553c + 021d93a commit a8a1352
Copy full SHA for a8a1352

File tree

1 file changed

+18
-12
lines changed
Filter options

1 file changed

+18
-12
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+18-12Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.