Skip to content

[Form] ChoiceType: Fix a notice when 'choices' normalizer is replaced #17406

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

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ public function configureOptions(OptionsResolver $resolver)
// forms)
$labels = $choiceLabels->labels;

// The $choiceLabels object is shared with the 'choices' closure.
// Since that normalizer can be replaced, labels have to be cleared here.
$choiceLabels->labels = array();

return function ($choice, $key) use ($labels) {
return $labels[$key];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\Tests\Fixtures\ChoiceSubType;

class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
{
Expand Down Expand Up @@ -1887,4 +1888,30 @@ public function testInitializeWithDefaultObjectChoice()
// Trigger data initialization
$form->getViewData();
}

/**
* This covers the case when:
* - Custom choice type added after a choice type.
* - Custom type is expanded.
* - Custom type replaces 'choices' normalizer with a custom one.
* In this case, custom type should not inherit labels from the first added choice type.
*/
public function testCustomChoiceTypeDoesNotInheritChoiceLabels()
{
$builder = $this->factory->createBuilder();
$builder->add('choice', 'choice', array(
'choices' => array(
'1' => '1',
'2' => '2',
),
)
);
$builder->add('subChoice', new ChoiceSubType());
$form = $builder->getForm();

// The default 'choices' normalizer would fill the $choiceLabels, but it has been replaced
// in the custom choice type, so $choiceLabels->labels remains empty array.
// In this case the 'choice_label' closure returns null and not the closure from the first choice type.
$this->assertNull($form->get('subChoice')->getConfig()->getOption('choice_label'));
}
}
51 changes: 51 additions & 0 deletions src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Paráda József <joczy.parada@gmail.com>
*/
class ChoiceSubType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('expanded' => true));
$resolver->setNormalizer('choices', function () {
return array(
'attr1' => 'Attribute 1',
'attr2' => 'Attribute 2',
);
});
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'sub_choice';
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'choice';
}
}