Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c71fd5f

Browse filesBrowse files
committed
[Form] Fixed overridden choices option in extended choice types
1 parent 16263fb commit c71fd5f
Copy full SHA for c71fd5f

File tree

7 files changed

+111
-5
lines changed
Filter options

7 files changed

+111
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php
+4-1Lines 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
}
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Form\Forms;
15+
use Symfony\Component\Form\Tests\Fixtures\ChoiceTypeExtension;
16+
17+
class ExtendedChoiceTypeTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider provideTestedTypes
21+
*/
22+
public function testChoicesAreOverriden($type)
23+
{
24+
$factory = Forms::createFormFactoryBuilder()
25+
->addTypeExtension(new ChoiceTypeExtension($type))
26+
->getFormFactory()
27+
;
28+
29+
$choices = $factory->create($type)->createView()->vars['choices'];
30+
31+
$this->assertCount(2, $choices);
32+
$this->assertSame('A', $choices[0]->label);
33+
$this->assertSame('a', $choices[0]->value);
34+
$this->assertSame('B', $choices[1]->label);
35+
$this->assertSame('b', $choices[1]->value);
36+
}
37+
38+
public function provideTestedTypes()
39+
{
40+
yield array(CountryTypeTest::TESTED_TYPE);
41+
yield array(CurrencyTypeTest::TESTED_TYPE);
42+
yield array(LanguageTypeTest::TESTED_TYPE);
43+
yield array(LocaleTypeTest::TESTED_TYPE);
44+
yield array(TimezoneTypeTest::TESTED_TYPE);
45+
}
46+
}
+45Lines 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 <fabien@symfony.com>
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.