8000 [Form] Fix `ChoiceType` with `choice_filter` and `expanded` options · symfony/symfony@b913930 · GitHub
[go: up one dir, main page]

Skip to content

Commit b913930

Browse files
committed
[Form] Fix ChoiceType with choice_filter and expanded options
1 parent a7e4494 commit b913930

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ protected function loadChoices(): iterable
5050
*/
5151
public function loadChoicesForValues(array $values, callable $value = null): array
5252
{
53-
return array_filter($this->decoratedLoader->loadChoicesForValues($values, $value), $this->filter);
53+
return array_filter(parent::loadChoicesForValues($values, $value), $this->filter);
5454
}
5555

5656
/**
5757
* {@inheritdoc}
5858
*/
5959
public function loadValuesForChoices(array $choices, callable $value = null): array
6060
{
61-
return $this->decoratedLoader->loadValuesForChoices(array_filter($choices, $this->filter), $value);
61+
return parent::loadValuesForChoices(array_filter($choices, $this->filter), $value);
6262
}
6363
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,31 @@ public function testSubmitSingleExpandedObjectChoices()
13211321
$this->assertNull($form[4]->getViewData());
13221322
}
13231323

1324+
public function testSubmitSingleExpandedFilteredObjectChoices()
1325+
{
1326+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1327+
'multiple' => false,
1328+
'expanded' => true,
1329+
'choices' => $this->objectChoices,
1330+
'choice_label' => 'name',
1331+
'choice_value' => 'id',
1332+
'choice_filter' => function ($choice) { return null === $choice || $choice->id < 4; },
1333+
]);
1334+
1335+
$form->submit('2');
1336+
1337+
$this->assertSame($this->objectChoices[1], $form->getData());
1338+
$this->assertTrue($form->isSynchronized());
1339+
1340+
$this->assertFalse($form[0]->getData());
1341+
$this->assertTrue($form[1]->getData());
1342+
$this->assertFalse($form[2]->getData());
1343+
$this->assertFalse(isset($form[3]));
1344+
$this->assertNull($form[0]->getViewData());
1345+
$this->assertSame('2', $form[1]->getViewData());
1346+
$this->assertNull($form[2]->getViewData());
1347+
}
1348+
13241349
public function testSubmitSingleExpandedClearMissingFalse()
13251350
{
13261351
$form = $this->factory->create(self::TESTED_TYPE, 'foo', [
@@ -1510,6 +1535,31 @@ public function testSubmitMultipleExpandedObjectChoices()
15101535
$this->assertNull($form[4]->getViewData());
15111536
}
15121537

1538+
public function testSubmitMultipleExpandedFilteredObjectChoices()
1539+
{
1540+
$form = $this->factory->create(static::TESTED_TYPE, null, [
1541+
'multiple' => true,
1542+
'expanded' => true,
1543+
'choices' => $this->objectChoices,
1544+
'choice_label' => 'name',
1545+
'choice_value' => 'id',
1546+
'choice_filter' => function ($choice) { return null === $choice || $choice->id < 4; },
1547+
]);
1548+
1549+
$form->submit(['1', '2']);
1550+
1551+
$this->assertSame([$this->objectChoices[0], $this->objectChoices[1]], $form->getData());
1552+
$this->assertTrue($form->isSynchronized());
1553+
1554+
$this->assertTrue($form[0]->getData());
1555+
$this->assertTrue($form[1]->getData());
1556+
$this->assertFalse($form[2]->getData());
1557+
$this->assertFalse(isset($form[3]));
1558+
$this->assertSame('1', $form[0]->getViewData());
1559+
$this->assertSame('2', $form[1]->getViewData());
1560+
$this->assertNull($form[2]->getViewData());
1561+
}
1562+
15131563
public function testSubmitMultipleChoicesInts()
15141564
{
15151565
$form = $this->factory->create(static::TESTED_TYPE, null, [

0 commit comments

Comments
 (0)
0