Skip to content

[Form] Fixed overridden choices option in extended choice types #21880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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
Expand All @@ -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,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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
Expand All @@ -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,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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
Expand All @@ -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,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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
Expand All @@ -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,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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
Expand All @@ -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,
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
45 changes: 45 additions & 0 deletions src/Symfony/Component/Form/Tests/Fixtures/ChoiceTypeExtension.php
Original file line number Diff line number Diff line change
@@ -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;
}
}