From 24c65bf8a84732c7a98a1df2e45a2044ef6fe26e Mon Sep 17 00:00:00 2001 From: Marvin Feldmann Date: Fri, 3 Jun 2022 18:53:44 +0200 Subject: [PATCH] Fix choice filter error when loading mix of grouped and non-grouped choices --- .../Loader/FilterChoiceLoaderDecorator.php | 13 ++++++++--- .../FilterChoiceLoaderDecoratorTest.php | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) 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']; 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