From 6f54ab96db625c7e5a8e9983c222602289e3f8fb Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 20 Dec 2021 11:43:33 +0000 Subject: [PATCH] [Form] Fix `ChoiceType` with `choice_filter` and `expanded` options --- .../Loader/FilterChoiceLoaderDecorator.php | 4 +- .../Extension/Core/Type/ChoiceTypeTest.php | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php b/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php index a52f3b82e432e..6a1122a695aa7 100644 --- a/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php +++ b/src/Symfony/Component/Form/ChoiceList/Loader/FilterChoiceLoaderDecorator.php @@ -50,7 +50,7 @@ protected function loadChoices(): iterable */ public function loadChoicesForValues(array $values, callable $value = null): array { - return array_filter($this->decoratedLoader->loadChoicesForValues($values, $value), $this->filter); + return array_filter(parent::loadChoicesForValues($values, $value), $this->filter); } /** @@ -58,6 +58,6 @@ public function loadChoicesForValues(array $values, callable $value = null): arr */ public function loadValuesForChoices(array $choices, callable $value = null): array { - return $this->decoratedLoader->loadValuesForChoices(array_filter($choices, $this->filter), $value); + return parent::loadValuesForChoices(array_filter($choices, $this->filter), $value); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 50b259f58e10e..db5dd3ee97b8c 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -1321,6 +1321,31 @@ public function testSubmitSingleExpandedObjectChoices() $this->assertNull($form[4]->getViewData()); } + public function testSubmitSingleExpandedFilteredObjectChoices() + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'multiple' => false, + 'expanded' => true, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + 'choice_filter' => function ($choice) { return null === $choice || $choice->id < 4; }, + ]); + + $form->submit('2'); + + $this->assertSame($this->objectChoices[1], $form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse(isset($form[3])); + $this->assertNull($form[0]->getViewData()); + $this->assertSame('2', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + } + public function testSubmitSingleExpandedClearMissingFalse() { $form = $this->factory->create(self::TESTED_TYPE, 'foo', [ @@ -1510,6 +1535,31 @@ public function testSubmitMultipleExpandedObjectChoices() $this->assertNull($form[4]->getViewData()); } + public function testSubmitMultipleExpandedFilteredObjectChoices() + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + 'choice_filter' => function ($choice) { return null === $choice || $choice->id < 4; }, + ]); + + $form->submit(['1', '2']); + + $this->assertSame([$this->objectChoices[0], $this->objectChoices[1]], $form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse(isset($form[3])); + $this->assertSame('1', $form[0]->getViewData()); + $this->assertSame('2', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + } + public function testSubmitMultipleChoicesInts() { $form = $this->factory->create(static::TESTED_TYPE, null, [