10000 [Form] Fix choices defined as Traversable by nicolas-grekas · Pull Request #16796 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix choices defined as Traversable #16796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Fix choices defined as Traversable
  • Loading branch information
nicolas-grekas committed Dec 5, 2015
commit 021d93a322baa307196d9d8278cb9cc5c654077a
30 changes: 18 additions & 12 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
4E7F
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ public function configureOptions(OptionsResolver $resolver)
return $choices;
}

ChoiceType::normalizeLegacyChoices($choices, $choiceLabels);
if (null === $choices) {
return;
}

return $choices;
return ChoiceType::normalizeLegacyChoices($choices, $choiceLabels);
};

// BC closure, to be removed in 3.0
Expand Down Expand Up @@ -520,26 +522,30 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
* are lost. Store them in a utility array that is used from the
* "choice_label" closure by default.
*
* @param array $choices The choice labels indexed by choices.
* Labels are replaced by generated keys.
* @param object $choiceLabels The object that receives the choice labels
* indexed by generated keys.
* @param int $nextKey The next generated key.
* @param array|\Traversable $choices The choice labels indexed by choices.
* @param object $choiceLabels The object that receives the choice labels
* indexed by generated keys.
* @param int $nextKey The next generated key.
*
* @return array The choices in a normalized array with labels replaced by generated keys.
*
* @internal Public only to be accessible from closures on PHP 5.3. Don't
* use this method as it may be removed without notice and will be in 3.0.
*/
public static function normalizeLegacyChoices(array &$choices, $choiceLabels, &$nextKey = 0)
public static function normalizeLegacyChoices($choices, $choiceLabels, &$nextKey = 0)
{
$normalizedChoices = array();

foreach ($choices as $choice => $choiceLabel) {
if (is_array($choiceLabel)) {
$choiceLabel = ''; // Dereference $choices[$choice]
self::normalizeLegacyChoices($choices[$choice], $choiceLabels, $nextKey);
if (is_array($choiceLabel) || $choiceLabel instanceof \Traversable) {
$normalizedChoices[$choice] = self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey);
continue;
}

$choiceLabels->labels[$nextKey] = $choiceLabel;
$choices[$choice] = $nextKey++;
$normalizedChoices[$choice] = $nextKey++;
}

return $normalizedChoices;
}
}
0