Skip to content

Commit b5b56fc

Browse files
committed
[Form] Deprecated usage of "choices" option in sub types
1 parent 1b63474 commit b5b56fc

File tree

10 files changed

+179
-6
lines changed

10 files changed

+179
-6
lines changed

UPGRADE-3.3.md

+28
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ Finder
127127

128128
* The `ExceptionInterface` has been deprecated and will be removed in 4.0.
129129

130+
Form
131+
----
132+
133+
* Using the "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``,
134+
``LocaleType``, and ``TimezoneType`` without overriding the ``choice_loader``
135+
option has been deprecated and will be ignored in 4.0.
136+
137+
Before:
138+
```php
139+
$builder->add('custom_locales', LocaleType::class, array(
140+
'choices' => $availableLocales,
141+
));
142+
```
143+
144+
After:
145+
```php
146+
$builder->add('custom_locales', LocaleType::class, array(
147+
'choices' => $availableLocales,
148+
'choice_loader' => null,
149+
));
150+
// or
151+
$builder->add('custom_locales', LocaleType::class, array(
152+
'choice_loader' => new CallbackChoiceLoader(function () {
153+
return $this->getAvailableLocales();
154+
}),
155+
));
156+
```
157+
130158
FrameworkBundle
131159
---------------
132160

UPGRADE-4.0.md

+25
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,31 @@ Form
187187
}
188188
```
189189

190+
* Using the "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``,
191+
``LocaleType``, and ``TimezoneType`` without overriding the ``choice_loader``
192+
option is now ignored.
193+
194+
Before:
195+
```php
196+
$builder->add('custom_locales', LocaleType::class, array(
197+
'choices' => $availableLocales,
198+
));
199+
```
200+
201+
After:
202+
```php
203+
$builder->add('custom_locales', LocaleType::class, array(
204+
'choices' => $availableLocales,
205+
'choice_loader' => null,
206+
));
207+
// or
208+
$builder->add('custom_locales', LocaleType::class, array(
209+
'choice_loader' => new CallbackChoiceLoader(function () {
210+
return $this->getAvailableLocales();
211+
}),
212+
));
213+
```
214+
190215
FrameworkBundle
191216
---------------
192217

src/Symfony/Component/Form/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* deprecated using "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``, ``LocaleType``, and
8+
``TimezoneType`` when "choice_loader" is not ``null``
79
* added `Symfony\Component\Form\FormErrorIterator::findByCodes()`
810
* added `getTypedExtensions`, `getTypes`, and `getTypeGuessers` to `Symfony\Component\Form\Test\FormIntegrationTestCase`
911
* added `FormPass`

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public function configureOptions(OptionsResolver $resolver)
3838
{
3939
$resolver->setDefaults(array(
4040
'choice_loader' => function (Options $options) {
41-
return $options['choices'] ? null : $this;
41+
if ($options['choices']) {
42+
@trigger_error(sprintf('Using the "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.', __CLASS__), E_USER_DEPRECATED);
43+
44+
return null;
45+
}
46+
47+
return $this;
4248
},
4349
'choice_translation_domain' => false,
4450
));

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public function configureOptions(OptionsResolver $resolver)
3838
{
3939
$resolver->setDefaults(array(
4040
'choice_loader' => function (Options $options) {
41-
return $options['choices'] ? null : $this;
41+
if ($options['choices']) {
42+
@trigger_error(sprintf('Using the "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.', __CLASS__), E_USER_DEPRECATED);
43+
44+
return null;
45+
}
46+
47+
return $this;
4248
},
4349
'choice_translation_domain' => false,
4450
));

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public function configureOptions(OptionsResolver $resolver)
3838
{
3939
$resolver->setDefaults(array(
4040
'choice_loader' => function (Options $options) {
41-
return $options['choices'] ? null : $this;
41+
if ($options['choices']) {
42+
@trigger_error(sprintf('Using the "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.', __CLASS__), E_USER_DEPRECATED);
43+
44+
return null;
45+
}
46+
47+
return $this;
4248
},
4349
'choice_translation_domain' => false,
4450
));

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public function configureOptions(OptionsResolver $resolver)
3838
{
3939
$resolver->setDefaults(array(
4040
'choice_loader' => function (Options $options) {
41-
return $options['choices'] ? null : $this;
41+
if ($options['choices']) {
42+
@trigger_error(sprintf('Using the "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.', __CLASS__), E_USER_DEPRECATED);
43+
44+
return null;
45+
}
46+
47+
return $this;
4248
},
4349
'choice_translation_domain' => false,
4450
));

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ public function configureOptions(OptionsResolver $resolver)
3535
{
3636
$resolver->setDefaults(array(
3737
'choice_loader' => function (Options $options) {
38-
return $options['choices'] ? null : $this;
38+
if ($options['choices']) {
39+
@trigger_error(sprintf('Using the "choices" option in %s has been deprecated since version 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.', __CLASS__), E_USER_DEPRECATED);
40+
41+
return null;
42+
}
43+
44+
return $this;
3945
},
4046
'choice_translation_domain' => false,
4147
));

src/Symfony/Component/Form/Tests/Extension/Core/Type/ExtendedChoiceTypeTest.php

+41-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Form\Forms;
1616
use Symfony\Component\Form\Tests\Fixtures\ChoiceTypeExtension;
17+
use Symfony\Component\Form\Tests\Fixtures\LazyChoiceTypeExtension;
1718

1819
class ExtendedChoiceTypeTest extends TestCase
1920
{
2021
/**
22+
* @group legacy
2123
* @dataProvider provideTestedTypes
2224
*/
23-
public function testChoicesAreOverridden($type)
25+
public function testLegacyChoicesAreOverridden($type)
2426
{
2527
$factory = Forms::createFormFactoryBuilder()
2628
->addTypeExtension(new ChoiceTypeExtension($type))
@@ -36,6 +38,44 @@ public function testChoicesAreOverridden($type)
3638
$this->assertSame('b', $choices[1]->value);
3739
}
3840

41+
/**
42+
* @dataProvider provideTestedTypes
43+
*/
44+
public function testChoicesAreOverridden($type)
45+
{
46+
$factory = Forms::createFormFactoryBuilder()
47+
->addTypeExtension(new ChoiceTypeExtension($type))
48+
->getFormFactory()
49+
;
50+
51+
$choices = $factory->create($type, null, array('choice_loader' => null))->createView()->vars['choices'];
52+
53+
$this->assertCount(2, $choices);
54+
$this->assertSame('A', $choices[0]->label);
55+
$this->assertSame('a', $choices[0]->value);
56+
$this->assertSame('B', $choices[1]->label);
57+
$this->assertSame('b', $choices[1]->value);
58+
}
59+
60+
/**
61+
* @dataProvider provideTestedTypes
62+
*/
63+
public function testChoiceLoaderIsOverridden($type)
64+
{
65+
$factory = Forms::createFormFactoryBuilder()
66+
->addTypeExtension(new LazyChoiceTypeExtension($type))
67+
->getFormFactory()
68+
;
69+
70+
$choices = $factory->create($type)->createView()->vars['choices'];
71+
72+
$this->assertCount(2, $choices);
73+
$this->assertSame('Lazy A', $choices[0]->label);
74+
$this->assertSame('lazy_a', $choices[0]->value);
75+
$this->assertSame('Lazy B', $choices[1]->label);
76+
$this->assertSame('lazy_b', $choices[1]->value);
77+
}
78+
3979
public function provideTestedTypes()
4080
{
4181
yield array(CountryTypeTest::TESTED_TYPE);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\ChoiceList\Loader\CallbackChoiceLoader;
16+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
19+
class LazyChoiceTypeExtension extends AbstractTypeExtension
20+
{
21+
private $extendedType;
22+
23+
public function __construct($extendedType = ChoiceType::class)
24+
{
25+
$this->extendedType = $extendedType;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function configureOptions(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefault('choice_loader', new CallbackChoiceLoader(function () {
34+
return array(
35+
'Lazy A' => 'lazy_a',
36+
'Lazy B' => 'lazy_b',
37+
);
38+
}));
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getExtendedType()
45+
{
46+
return $this->extendedType;
47+
}
48+
}

0 commit comments

Comments
 (0)