[Form] Fixed overridden choices option in extended choice types
This commit is contained in:
parent
cc5c233f15
commit
bcda2c2d8e
@ -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,
|
||||
));
|
||||
}
|
||||
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user