-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fixed: Duplicate choice labels are remembered when using "choices_as_values" = false #16685
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -238,6 +238,7 @@ public function finishView(FormView $view, FormInterface $form, array $options) | |
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$choiceLabels = array(); | ||
$choiceListFactory = $this->choiceListFactory; | ||
|
||
$emptyData = function (Options $options) { | ||
|
@@ -252,6 +253,44 @@ public function configureOptions(OptionsResolver $resolver) | |
return $options['required'] ? null : ''; | ||
}; | ||
|
||
// BC closure, to be removed in 3.0 | ||
$choicesNormalizer = function (Options $options, $choices) use (&$choiceLabels) { | ||
// Unset labels from previous invocations | ||
$choiceLabels = array(); | ||
|
||
// This closure is irrelevant when "choices_as_values" is set to true | ||
if ($options['choices_as_values']) { | ||
return $choices; | ||
} | ||
|
||
ChoiceType::normalizeLegacyChoices($choices, $choiceLabels); | ||
|
||
return $choices; | ||
}; | ||
|
||
// BC closure, to be removed in 3.0 | ||
$choiceLabel = function (Options $options) use (&$choiceLabels) { | ||
// If the choices contain duplicate labels, the normalizer of the | ||
// "choices" option stores them in the $choiceLabels variable | ||
|
||
// Trigger the normalizer | ||
$options->offsetGet('choices'); | ||
|
||
// Pick labels from $choiceLabels if available | ||
// Don't invoke count() to avoid creating a copy of the array (yet) | ||
if ($choiceLabels) { | ||
// Don't pass the labels by reference. We do want to create a | ||
// copy here so that every form has an own version of that | ||
// variable (contrary to the global reference shared by all | ||
// forms) | ||
return function ($choice, $key) use ($choiceLabels) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this may create a copy too (assuming we avoid the copy just above) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is without a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this use a reference too, to avoid copying the array ? |
||
return $choiceLabels[$key]; | ||
}; | ||
} | ||
|
||
return; | ||
}; | ||
|
||
$choiceListNormalizer = function (Options $options, $choiceList) use ($choiceListFactory) { | ||
if ($choiceList) { | ||
@trigger_error('The "choice_list" option is deprecated since version 2.7 and will be removed in 3.0. Use "choice_loader" instead.', E_USER_DEPRECATED); | ||
|
@@ -322,7 +361,7 @@ public function configureOptions(OptionsResolver $resolver) | |
'choices' => array(), | ||
'choices_as_values' => false, | ||
'choice_loader' => null, | ||
'choice_label' => null, | ||
'choice_label' => $choiceLabel, | ||
'choice_name' => null, | ||
'choice_value' => null, | ||
'choice_attr' => null, | ||
|
@@ -340,6 +379,7 @@ public function configureOptions(OptionsResolver $resolver) | |
'choice_translation_domain' => true, | ||
)); | ||
|
||
$resolver->setNormalizer('choices', $choicesNormalizer); | ||
$resolver->setNormalizer('choice_list', $choiceListNormalizer); | ||
$resolver->setNormalizer('placeholder', $placeholderNormalizer); | ||
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); | ||
|
@@ -454,4 +494,34 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op | |
$options['choice_attr'] | ||
); | ||
} | ||
|
||
/** | ||
* When "choices_as_values" is set to false, the choices are in the keys and | ||
* their labels in the values. Labels may occur twice. The form component | ||
* flips the choices array in the new implementation, so duplicate labels | ||
* 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 array $choiceLabels The array that receives the choice labels | ||
* indexed by generated keys. | ||
* @param int|null $nextKey The next generated key. | ||
* | ||
* @internal Public only to be accessible from closures on PHP 5.3. Don't | ||
* use this method, as it may be removed without notice. | ||
*/ | ||
public static function normalizeLegacyChoices(array &$choices, array &$choiceLabels, &$nextKey = 0) | ||
{ | ||
foreach ($choices as $choice => &$choiceLabel) { | ||
if (is_array($choiceLabel)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't this force to copy the zval as it is a reference ? |
||
self::normalizeLegacyChoices($choiceLabel, $choiceLabels, $nextKey); | ||
continue; | ||
} | ||
|
||
$choiceLabels[$nextKey] = $choiceLabel; | ||
$choices[$choice] = $nextKey; | ||
++$nextKey; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should remove the
&
, it creates an unnecessary ref mismatchor is it really required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, the answer is yes, there is a reason to keep the
&
, which means the count below has to be removed to prevent a useless memory duplicationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, this is not really beautiful. But the closures need to access the same variable to be able to properly exchange information.