bug #21880 [Form] Fixed overridden choices option in extended choice types (HeahDude)

This PR was merged into the 3.2 branch.

Discussion
----------

[Form] Fixed overridden choices option in extended choice types

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20771
| License       | MIT
| Doc PR        | ~

Commits
-------

bcda2c2d8e [Form] Fixed overridden choices option in extended choice types
This commit is contained in:
Fabien Potencier 2017-03-06 09:46:59 -08:00
commit eae76953d6
7 changed files with 112 additions and 5 deletions

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CountryType extends AbstractType implements ChoiceLoaderInterface
@ -36,7 +37,9 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
},
'choice_translation_domain' => false,
));
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
@ -36,7 +37,9 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
},
'choice_translation_domain' => false,
));
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LanguageType extends AbstractType implements ChoiceLoaderInterface
@ -36,7 +37,9 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
},
'choice_translation_domain' => false,
));
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LocaleType extends AbstractType implements ChoiceLoaderInterface
@ -36,7 +37,9 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
},
'choice_translation_domain' => false,
));
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TimezoneType extends AbstractType implements ChoiceLoaderInterface
@ -33,7 +34,9 @@ class TimezoneType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
return $options['choices'] ? null : $this;
},
'choice_translation_domain' => false,
));
}

View File

@ -0,0 +1,47 @@
<?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\Extension\Core\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Tests\Fixtures\ChoiceTypeExtension;
class ExtendedChoiceTypeTest extends TestCase
{
/**
* @dataProvider provideTestedTypes
*/
public function testChoicesAreOverridden($type)
{
$factory = Forms::createFormFactoryBuilder()
->addTypeExtension(new ChoiceTypeExtension($type))
->getFormFactory()
;
$choices = $factory->create($type)->createView()->vars['choices'];
$this->assertCount(2, $choices);
$this->assertSame('A', $choices[0]->label);
$this->assertSame('a', $choices[0]->value);
$this->assertSame('B', $choices[1]->label);
$this->assertSame('b', $choices[1]->value);
}
public function provideTestedTypes()
{
yield array(CountryTypeTest::TESTED_TYPE);
yield array(CurrencyTypeTest::TESTED_TYPE);
yield array(LanguageTypeTest::TESTED_TYPE);
yield array(LocaleTypeTest::TESTED_TYPE);
yield array(TimezoneTypeTest::TESTED_TYPE);
}
}

View File

@ -0,0 +1,45 @@
<?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\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ChoiceTypeExtension extends AbstractTypeExtension
{
private $extendedType;
public function __construct($extendedType = ChoiceType::class)
{
$this->extendedType = $extendedType;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('choices', array(
'A' => 'a',
'B' => 'b',
));
}
/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return $this->extendedType;
}
}