8000 [Form] Fixed: Duplicate choice labels are remembered when using "choices_as_values" = false by webmozart · Pull Request #16685 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Merged
merged 1 commit into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
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
72 changes: 71 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Copy link
Member

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 mismatch
or is it really required?

Copy link
Member

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 duplication

Copy link
Contributor Author

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.

// 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding an & may be necessary I agree

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is without a & by intention. The outer $choiceLabels lives in the type's scope, i.e. every field uses the same variable to transfer information from choices to choice_label during its construction. The inner closure, however, lives in the scope of the field and is used to generate ChoiceView instances during view construction. If we'd use the global variable instead, all choice fields in a form would have the labels of the last one, which is not what we want.

Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,23 @@ public function testPassChoiceDataToView()
), $view->vars['choices']);
}

/**
* @group legacy
*/
public function testDuplicateChoiceLabels()
{
$form = $this->factory->create('choice', null, array(
'choices' => array('a' => 'A', 'b' => 'B', 'c' => 'A'),
));
$view = $form->createView();

$this->assertEquals(array(
new ChoiceView('a', 'a', 'A'),
new ChoiceView('b', 'b', 'B'),
new ChoiceView('c', 'c', 'A'),
), $view->vars['choices']);
}

public function testAdjustFullNameForMultipleNonExpanded()
{
$form = $this->factory->createNamed('name', 'choice', null, array(
Expand Down
0