Skip to content

Commit

Permalink
feature #5332 Allow to pass no choices no the Choice field (javieregu…
Browse files Browse the repository at this point in the history
…iluz)

This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Allow to pass no choices no the Choice field

Fixes #3688.

Folks doing advanced things with JS (e.g. dependent lists) might need to add an empty choice field which is populated later.

Commits
-------

6fe5f92 Allow to pass no choices no the Choice field
  • Loading branch information
javiereguiluz committed Jul 14, 2022
2 parents 0b2b446 + 6fe5f92 commit 660d1bd
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 6 deletions.
3 changes: 0 additions & 3 deletions src/Field/Configurator/ChoiceConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
$isMultipleChoice = $field->getCustomOption(ChoiceField::OPTION_ALLOW_MULTIPLE_CHOICES);

$choices = $this->getChoices($field->getCustomOption(ChoiceField::OPTION_CHOICES), $entityDto, $field);
if (empty($choices)) {
throw new \InvalidArgumentException(sprintf('The "%s" choice field must define its possible choices using the setChoices() method.', $field->getProperty()));
}

if ($areChoicesTranslatable) {
$field->setFormTypeOptionIfNotSet('choices', array_keys($choices));
Expand Down
4 changes: 1 addition & 3 deletions tests/Field/ChoiceFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ protected function setUp(): void

public function testFieldWithoutChoices()
{
$this->expectException(\InvalidArgumentException::class);

$field = ChoiceField::new('foo');
$this->configure($field);
self::assertSame([], $this->configure($field)->getFormTypeOption(ChoiceField::OPTION_CHOICES));
}

public function testFieldWithChoiceGeneratorCallback()
Expand Down

0 comments on commit 660d1bd

Please sign in to comment.