8000 [Form] Fix `ChoiceType` with `choice_filter` and `expanded` options by cs278 · Pull Request #44725 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix ChoiceType with choice_filter and expanded options #44725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file 8000
Failed to load files.
Loading
Diff view
Diff view
[Form] Fix ChoiceType with choice_filter and expanded options
  • Loading branch information
cs278 authored and xabbuh committed Apr 12, 2022
commit 6f54ab96db625c7e5a8e9983c222602289e3f8fb
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ 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);
}

/**
* {@inheritdoc}
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -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, [
Expand Down
0