Skip to content

Commit 85ff6f4

Browse files
committed
[Form] Fix EnumType choice_label logic for grouped choices
1 parent 502eb5e commit 85ff6f4

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/Symfony/Component/Form/Extension/Core/Type/EnumType.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,21 @@ public function configureOptions(OptionsResolver $resolver): void
3131
->setAllowedValues('class', enum_exists(...))
3232
->setDefault('choices', static fn (Options $options): array => $options['class']::cases())
3333
->setDefault('choice_label', static function (Options $options) {
34-
if (\is_array($options['choices']) && !array_is_list($options['choices'])) {
35-
return null;
34+
$choices = $options['choices'];
35+
36+
if (\is_array($choices) && !array_is_list($choices)) {
37+
// Check for grouped choices
38+
$isGrouped = false;
39+
foreach ($choices as $value) {
40+
if (\is_array($value) && array_is_list($value)) {
41+
$isGrouped = true;
42+
break;
43+
}
44+
}
45+
if (!$isGrouped) {
46+
// Associative array = custom labels
47+
return null;
48+
}
3649
}
3750

3851
return static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name;

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,69 @@ public function testChoicesWithLabels()
311311
$this->assertSame('no', $view->children[1]->vars['label']);
312312
}
313313

314+
public function testGroupedEnumChoices()
315+
{
316+
$form = $this->factory->create($this->getTestedType(), null, [
317+
'multiple' => false,
318+
'expanded' => true,
319+
'class' => Answer::class,
320+
'choices' => [
321+
'Group 1' => [Answer::Yes, Answer::No],
322+
'Group 2' => [Answer::FourtyTwo],
323+
],
324+
]);
325+
$view = $form->createView();
326+
$this->assertCount(2, $view->vars['choices']['Group 1']->choices);
327+
$this->assertSame('Yes', $view->vars['choices']['Group 1']->choices[0]->label);
328+
$this->assertSame('No', $view->vars['choices']['Group 1']->choices[1]->label);
329+
$this->assertCount(1, $view->vars['choices']['Group 2']->choices);
330+
$this->assertSame('FourtyTwo', $view->vars['choices']['Group 2']->choices[2]->label);
331+
}
332+
333+
public function testEnumChoicesWithCustomLabels()
334+
{
335+
$form = $this->factory->create($this->getTestedType(), null, [
336+
'multiple' => false,
337+
'expanded' => true,
338+
'class' => Answer::class,
339+
'choices' => [
340+
'Custom Yes' => Answer::Yes,
341+
'Custom No' => Answer::No,
342+
],
343+
]);
344+
$view = $form->createView();
345+
$this->assertSame('Custom Yes', $view->children[0]->vars['label']);
346+
$this->assertSame('Custom No', $view->children[1]->vars['label']);
347+
}
348+
349+
public function testGroupedEnumChoicesWithCustomLabels()
350+
{
351+
$form = $this->factory->create($this->getTestedType(), null, [
352+
'multiple' => false,
353+
'expanded' => true,
354+
'class' => Answer::class,
355+
'choices' => [
356+
'Group 1' => [
357+
'Custom Yes' => Answer::Yes,
358+
'Custom No' => Answer::No,
359+
],
360+
'Group 2' => [
361+
'Custom 42' => Answer::FourtyTwo,
362+
],
363+
],
364+
]);
365+
$view = $form->createView();
366+
367+
// Test Group 1
368+
$this->assertCount(2, $view->vars['choices']['Group 1']->choices);
369+
$this->assertSame('Custom Yes', $view->vars['choices']['Group 1']->choices[0]->label);
370+
$this->assertSame('Custom No', $view->vars['choices']['Group 1']->choices[1]->label);
371+
372+
// Test Group 2
373+
$this->assertCount(1, $view->vars['choices']['Group 2']->choices);
374+
$this->assertSame('Custom 42', $view->vars['choices']['Group 2']->choices[2]->label);
375+
}
376+
314377
protected function getTestOptions(): array
315378
{
316379
return ['class' => Suit::class];

0 commit comments

Comments
 (0)