8000 [Form] Deprecated usage of "choices" option in sub types · symfony/symfony@7ba98df · GitHub
[go: up one dir, main page]

Skip to content
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 7ba98df

Browse files
committed
[Form] Deprecated usage of "choices" option in sub types
1 parent d236af6 commit 7ba98df

File tree

10 files changed

+179
-6
lines changed

10 files changed

+179
-6
lines changed

UPGRADE-3.3.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,34 @@ Finder
114114

115115
* The `ExceptionInterface` has been deprecated and will be removed in 4.0.
116116

117+
Form
118+
------
119+
120+
* Using the "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``,
121+
``LocaleType``, and ``TimezoneType`` has been deprecated and will be ignored in 4.0.
122+
Use the "choice_loader" option instead or explicitly set it to ``null``.
123+
124+
Before:
125+
```php
126+
$builder->add('custom_locales', LocaleType::class, array(
127+
'choices' => $availableLocales,
128+
));
129+
```
130+
131+
After:
132+
```php
133+
$builder->add('custom_locales', LocaleType::class, array(
134+
'choices' => $availableLocales,
135+
'choice_loader' => null,
136+
));
137+
// or
138+
$builder->add('custom_locales', LocaleType::class, array(
139+
'choice_loader' => new CallbackChoiceLoader(function () {
140+
return $this->getAvailableLocales();
141+
}),
142+
));
143+
```
144+
117145
FrameworkBundle
118146
---------------
119147

UPGRADE-4.0.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,31 @@ Form
184184
}
185185
```
186186

187+
* Using the "choices" option in ``CountryType``, ``CurrencyType``, ``LanguageType``,
188+
``LocaleType``, and ``TimezoneType`` is now ignored. Use the "choice_loader" option
189+
instead or explicitly set it to ``null``.
190+
191+
Before:
192+
```php
193+
$builder->add('custom_locales', LocaleType::class, array(
194+
'choices' => $availableLocales,
195+
));
196+
```
197+
198+
After:
199+
```php
200+
$builder->add('custom_locales', LocaleType::class, array(
201+
'choices' => $availableLocales,
202+
'choice_loader' => null,
203+
));
204+
// or
205+
$builder->add('custom_locales', LocaleType::class, array(
206+
'choice_loader' => new CallbackChoiceLoader(function () {
207+
return $this->getAvailableLocales();
208+
}),
209+
));
210+
```
211+
187212
FrameworkBundle
188213
---------------
189214

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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 `getTypedExtensions`, `getTypes`, and `getTypeGuessers` to `Symfony\Component\Form\Test\FormIntegrationTestCase`
810
* added `FormPass`
911

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

Lines changed: 7 additions & 1 deletion
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 "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

Lines changed: 7 additions & 1 deletion
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 "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

Lines changed: 7 additions & 1 deletion
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 "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

Lines changed: 7 additions & 1 deletion
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 "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

Lines changed: 7 additions & 1 deletion
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 "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

Lines changed: 41 additions & 1 deletion
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);
Lines changed: 48 additions & 0 deletions
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)
0