Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eae7695

Browse files
committed
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 ------- bcda2c2 [Form] Fixed overridden choices option in extended choice types
2 parents cc5c233 + bcda2c2 commit eae7695

File tree

7 files changed

+112
-5
lines changed

7 files changed

+112
-5
lines changed

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
class CountryType extends AbstractType implements ChoiceLoaderInterface
@@ -36,7 +37,9 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
3637
public function configureOptions(OptionsResolver $resolver)
3738
{
3839
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
40+
'choice_loader' => function (Options $options) {
41+
return $options['choices'] ? null : $this;
42+
},
4043
'choice_translation_domain' => false,
4144
));
4245
}

src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
@@ -36,7 +37,9 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
3637
public function configureOptions(OptionsResolver $resolver)
3738
{
3839
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
40+
'choice_loader' => function (Options $options) {
41+
return $options['choices'] ? null : $this;
42+
},
4043
'choice_translation_domain' => false,
4144
));
4245
}

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
class LanguageType extends AbstractType implements ChoiceLoaderInterface
@@ -36,7 +37,9 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
3637
public function configureOptions(OptionsResolver $resolver)
3738
{
3839
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
40+
'choice_loader' => function (Options $options) {
41+
return $options['choices'] ? null : $this;
42+
},
4043
'choice_translation_domain' => false,
4144
));
4245
}

src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1717
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
class LocaleType extends AbstractType implements ChoiceLoaderInterface
@@ -36,7 +37,9 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
3637
public function configureOptions(OptionsResolver $resolver)
3738
{
3839
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
40+
'choice_loader' => function (Options $options) {
41+
return $options['choices'] ? null : $this;
42+
},
4043
'choice_translation_domain' => false,
4144
));
4245
}

src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\OptionsResolver\Options;
1718
use Symfony\Component\OptionsResolver\OptionsResolver;
1819

1920
class TimezoneType extends AbstractType implements ChoiceLoaderInterface
@@ -33,7 +34,9 @@ class TimezoneType extends AbstractType implements ChoiceLoaderInterface
3334
public function configureOptions(OptionsResolver $resolver)
3435
{
3536
$resolver->setDefaults(array(
36-
'choice_loader' => $this,
37+
'choice_loader' => function (Options $options) {
38+
return $options['choices'] ? null : $this;
39+
},
3740
'choice_translation_domain' => false,
3841
));
3942
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Form\Forms;
16+
use Symfony\Component\Form\Tests\Fixtures\ChoiceTypeExtension;
17+
18+
class ExtendedChoiceTypeTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider provideTestedTypes
22+
*/
23+
public function testChoicesAreOverridden($type)
24+
{
25+
$factory = Forms::createFormFactoryBuilder()
26+
->addTypeExtension(new ChoiceTypeExtension($type))
27+
->getFormFactory()
28+
;
29+
30+
$choices = $factory->create($type)->createView()->vars['choices'];
31+
32+
$this->assertCount(2, $choices);
33+
$this->assertSame('A', $choices[0]->label);
34+
$this->assertSame('a', $choices[0]->value);
35+
$this->assertSame('B', $choices[1]->label);
36+
$this->assertSame('b', $choices[1]->value);
37+
}
38+
39+
public function provideTestedTypes()
40+
{
41+
yield array(CountryTypeTest::TESTED_TYPE);
42+
yield array(CurrencyTypeTest::TESTED_TYPE);
43+
yield array(LanguageTypeTest::TESTED_TYPE);
44+
yield array(LocaleTypeTest::TESTED_TYPE);
45+
yield array(TimezoneTypeTest::TESTED_TYPE);
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures;
13+
14+
use Symfony\Component\Form\AbstractTypeExtension;
15+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
class ChoiceTypeExtension extends AbstractTypeExtension
19+
{
20+
private $extendedType;
21+
22+
public function __construct($extendedType = ChoiceType::class)
23+
{
24+
$this->extendedType = $extendedType;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function configureOptions(OptionsResolver $resolver)
31+
{
32+
$resolver->setDefault('choices', array(
33+
'A' => 'a',
34+
'B' => 'b',
35+
));
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getExtendedType()
42+
{
43+
return $this->extendedType;
44+
}
45+
}

0 commit comments

Comments
 (0)