diff --git a/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php b/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php index a52f3b82e432e..250d6d2a146db 100644 --- a/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php +++ b/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php @@ -36,10 +36,17 @@ protected function loadChoices(): iterable } foreach ($structuredValues as $group => $values) { - if ($values && $filtered = array_filter($list->getChoicesForValues($values), $this->filter)) { - $choices[$group] = $filtered; + if (is_array($values)) { + if ($values && $filtered = array_filter($list->getChoicesForValues($values), $this->filter)) { + $choices[$group] = $filtered; + } + continue; + // filter empty groups + } + + if ($filtered = array_filter($list->getChoicesForValues([$values]), $this->filter)) { + $choices[$group] = $filtered[0]; } - // filter empty groups } return $choices ?? []; diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php index d4cc3ce72a4d0..1f91a47275a33 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Loader/FilterChoiceLoaderDecoratorTest.php @@ -47,6 +47,29 @@ public function testLoadChoiceListWithGroupedChoices() ]), $loader->loadChoiceList()); } + public function testLoadChoiceListMixedWithGroupedAndNonGroupedChoices() + { + $filter = function ($choice) { + return 0 === $choice % 2; + }; + + $choices = array_merge(range(1, 9), ['grouped' => range(10, 40, 5)]); + $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader($choices), $filter); + + $this->assertEquals(new ArrayChoiceList([ + 1 => 2, + 3 => 4, + 5 => 6, + 7 => 8, + 'grouped' => [ + 0 => 10, + 2 => 20, + 4 => 30, + 6 => 40, + ], + ]), $loader->loadChoiceList()); + } + public function testLoadValuesForChoices() { $evenValues = [1 => '2', 3 => '4'];