Skip to content

Commit f05f575

Browse files
committed
[Form] Deprecated choice_attr as nested arrays mapped by indexes
1 parent 3987914 commit f05f575

File tree

9 files changed

+888
-21
lines changed

9 files changed

+888
-21
lines changed

UPGRADE-5.1.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,49 @@ Form
2424
* Implementing the `FormConfigBuilderInterface` without implementing the `setIsEmptyCallback()` method
2525
is deprecated. The method will be added to the interface in 6.0.
2626
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()` - not defining them is deprecated.
27+
* Usage of `choice_attr` option as an array of nested arrays has been deprecated and indexes will be considered as attributes in 6.0. Use a unique array for all choices or a `callable` instead.
28+
29+
Before:
30+
```php
31+
// Single array for all choices using callable
32+
'choice_attr' => function () {
33+
return ['class' => 'choice-options'];
34+
},
35+
36+
// Different arrays per choice using array
37+
'choices' => [
38+
'Yes' => true,
39+
'No' => false,
40+
'Maybe' => null,
41+
],
42+
'choice_attr' => [
43+
'Yes' => ['class' => 'option-green'],
44+
'No' => ['class' => 'option-red'],
45+
],
46+
```
47+
48+
After:
49+
```php
50+
// Single array for all choices using array
51+
'choice_attr' => ['class' => 'choice-options'],
52+
53+
// Different arrays per choice using callable
54+
'choices' => [
55+
'Yes' => true,
56+
'No' => false,
57+
'Maybe' => null,
58+
],
59+
'choice_attr' => function ($choice, $index, $value) {
60+
if ('Yes' === $index) {
61+
return ['class' => 'option-green'];
62+
}
63+
if ('No' === $index) {
64+
return ['class' => 'option-red'];
65+
}
66+
67+
return [];
68+
},
69+
```
2770

2871
FrameworkBundle
2972
---------------

UPGRADE-6.0.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,50 @@ Form
2222
* Added the `getIsEmptyCallback()` method to the `FormConfigInterface`.
2323
* Added the `setIsEmptyCallback()` method to the `FormConfigBuilderInterface`.
2424
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()`.
25+
* Usage of `choice_attr` option as an array of nested arrays has been removed
26+
and indexes are considered as attributes. Use a unique array for all choices or a `callable` instead.
27+
28+
Before:
29+
```php
30+
// Single array for all choices using callable
31+
'choice_attr' => function () {
32+
return ['class' => 'choice-options'];
33+
},
34+
35+
// Different arrays per choice using array
36+
'choices' => [
37+
'Yes' => true,
38+
'No' => false,
39+
'Maybe' => null,
40+
],
41+
'choice_attr' => [
42+
'Yes' => ['class' => 'option-green'],
43+
'No' => ['class' => 'option-red'],
44+
],
45+
```
46+
47+
After:
48+
```php
49+
// Single array for all choices using array
50+
'choice_attr' => ['class' => 'choice-options'],
51+
52+
// Different arrays per choice using callable
53+
'choices' => [
54+
'Yes' => true,
55+
'No' => false,
56+
'Maybe' => null,
57+
],
58+
'choice_attr' => function ($choice, $index, $value) {
59+
if ('Yes' === $index) {
60+
return ['class' => 'option-green'];
61+
}
62+
if ('No' === $index) {
63+
return ['class' => 'option-red'];
64+
}
65+
66+
return [];
67+
},
68+
```
2569

2670
FrameworkBundle
2771
---------------

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3HorizontalLayoutTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

14+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15+
1416
abstract class AbstractBootstrap3HorizontalLayoutTest extends AbstractBootstrap3LayoutTest
1517
{
1618
public function testLabelOnForm()
@@ -212,6 +214,42 @@ public function testCheckboxRowWithHelp()
212214
./span[text() = "[trans]really helpful text[/trans]"]
213215
]
214216
]
217+
'
218+
);
219+
}
220+
221+
public function testSingleChoiceExpandedAttributes()
222+
{
223+
$form = $this->factory->createNamed('name', ChoiceType::class, '&a', [
224+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
225+
'choice_attr' => ['class' => 'foo&bar'],
226+
'multiple' => false,
227+
'expanded' => true,
228+
]);
229+
230+
$this->assertWidgetMatchesXpath($form->createView(), [],
231+
'/div
232+
[
233+
./div
234+
[@class="radio"]
235+
[
236+
./label
237+
[.=" [trans]Choice&A[/trans]"]
238+
[
239+
input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked][@class="foo&bar"]
240+
]
241+
]
242+
/following-sibling::div
243+
[@class="radio"]
244+
[
245+
./label
246+
[.=" [trans]Choice&B[/trans]"]
247+
[
248+
input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)][@class="foo&bar"]
249+
]
250+
]
251+
/following-sibling::input[@type="hidden"][@id="name__token"]
252+
]
215253
'
216254
);
217255
}

0 commit comments

Comments
 (0)