diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index ef9a4b5baed00..515f104af692c 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -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]; }; diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index d7536f7640d3d..cf74fec212ec5 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -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 { @@ -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')); + } } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php b/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php new file mode 100644 index 0000000000000..857dd5d5a77eb --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php @@ -0,0 +1,51 @@ + + * + * 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 + */ +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'; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy