Skip to content

Commit 24c65bf

Browse files
committed
Fix choice filter error when loading mix of grouped and non-grouped choices
1 parent 1457491 commit 24c65bf

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ protected function loadChoices(): iterable
3636
}
3737

3838
foreach ($structuredValues as $group => $values) {
39-
if ($values && $filtered = array_filter($list->getChoicesForValues($values), $this->filter)) {
40-
$choices[$group] = $filtered;
39+
if (is_array($values)) {
40+
if ($values && $filtered = array_filter($list->getChoicesForValues($values), $this->filter)) {
41+
$choices[$group] = $filtered;
42+
}
43+
continue;
44+
// filter empty groups
45+
}
46+
47+
if ($filtered = array_filter($list->getChoicesForValues([$values]), $this->filter)) {
48+
$choices[$group] = $filtered[0];
4149
}
42-
// filter empty groups
4350
}
4451

4552
return $choices ?? [];

src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ public function testLoadChoiceListWithGroupedChoices()
4747
]), $loader->loadChoiceList());
4848
}
4949

50+
public function testLoadChoiceListMixedWithGroupedAndNonGroupedChoices()
51+
{
52+
$filter = function ($choice) {
53+
return 0 === $choice % 2;
54+
};
55+
56+
$choices = array_merge(range(1, 9), ['grouped' => range(10, 40, 5)]);
57+
$loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader($choices), $filter);
58+
59+
$this->assertEquals(new ArrayChoiceList([
60+
1 => 2,
61+
3 => 4,
62+
5 => 6,
63+
7 => 8,
64+
'grouped' => [
65+
0 => 10,
66+
2 => 20,
67+
4 => 30,
68+
6 => 40,
69+
],
70+
]), $loader->loadChoiceList());
71+
}
72+
5073
public function testLoadValuesForChoices()
5174
{
5275
$evenValues = [1 => '2', 3 => '4'];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy