Skip to content
Merged
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
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public function configureOptions(OptionsResolver $resolver): void
->setAllowedTypes('class', 'string')
->setAllowedValues('class', enum_exists(...))
->setDefault('choices', static fn (Options $options): array => $options['class']::cases())
->setDefault('choice_label', static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name)
->setDefault('choice_label', static function (Options $options) {
if (\is_array($options['choices']) && !array_is_list($options['choices'])) {
return null;
}

return static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name;
})
Comment on lines +33 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh That change seems to break BackedEnum implementing TranslatableInterface?
See EasyCorp/EasyAdminBundle#7243

->setDefault('choice_value', static function (Options $options): ?\Closure {
if (!is_a($options['class'], \BackedEnum::class, true)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,43 @@ public function testChoiceLabelTranslatable()
$this->assertEquals('Left', $view->children[0]->vars['label']->trans(new IdentityTranslator()));
}

public function testChoices()
{
$form = $this->factory->create($this->getTestedType(), null, [
'multiple' => false,
'expanded' => true,
'class' => Answer::class,
'choices' => [
Answer::Yes,
Answer::No,
],
]);

$view = $form->createView();

$this->assertCount(2, $view->children);
$this->assertSame('Yes', $view->children[0]->vars['label']);
$this->assertSame('No', $view->children[1]->vars['label']);
}

public function testChoicesWithLabels()
{
$form = $this->factory->create($this->getTestedType(), null, [
'multiple' => false,
'expanded' => true,
'class' => Answer::class,
'choices' => [
'yes' => Answer::Yes,
'no' => Answer::No,
],
]);

$view = $form->createView();

$this->assertSame('yes', $view->children[0]->vars['label']);
$this->assertSame('no', $view->children[1]->vars['label']);
}

protected function getTestOptions(): array
{
return ['class' => Suit::class];
Expand Down
Loading