Skip to content

Commit f35f67f

Browse files
Merge branch '6.4' into 7.3
* 6.4: fix compatibility with Relay 0.12 [Translation] Update Danish (da) translations - Remove needs-review-translation state from validated translations in security.da.xlf and validators.da.xlf keep labels from configured choices
2 parents 20ad562 + 54ff91a commit f35f67f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Extension/Core/Type/EnumType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ public function configureOptions(OptionsResolver $resolver): void
3030
->setAllowedTypes('class', 'string')
3131
->setAllowedValues('class', enum_exists(...))
3232
->setDefault('choices', static fn (Options $options): array => $options['class']::cases())
33-
->setDefault('choice_label', static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name)
33+
->setDefault('choice_label', static function (Options $options) {
34+
if (\is_array($options['choices']) && !array_is_list($options['choices'])) {
35+
return null;
36+
}
37+
38+
return static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name;
39+
})
3440
->setDefault('choice_value', static function (Options $options): ?\Closure {
3541
if (!is_a($options['class'], \BackedEnum::class, true)) {
3642
return null;

Tests/Extension/Core/Type/EnumTypeTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,43 @@ public function testChoiceLabelTranslatable()
274274
$this->assertEquals('Left', $view->children[0]->vars['label']->trans(new IdentityTranslator()));
275275
}
276276

277+
public function testChoices()
278+
{
279+
$form = $this->factory->create($this->getTestedType(), null, [
280+
'multiple' => false,
281+
'expanded' => true,
282+
'class' => Answer::class,
283+
'choices' => [
284+
Answer::Yes,
285+
Answer::No,
286+
],
287+
]);
288+
289+
$view = $form->createView();
290+
291+
$this->assertCount(2, $view->children);
292+
$this->assertSame('Yes', $view->children[0]->vars['label']);
293+
$this->assertSame('No', $view->children[1]->vars['label']);
294+
}
295+
296+
public function testChoicesWithLabels()
297+
{
298+
$form = $this->factory->create($this->getTestedType(), null, [
299+
'multiple' => false,
300+
'expanded' => true,
301+
'class' => Answer::class,
302+
'choices' => [
303+
'yes' => Answer::Yes,
304+
'no' => Answer::No,
305+
],
306+
]);
307+
308+
$view = $form->createView();
309+
310+
$this->assertSame('yes', $view->children[0]->vars['label']);
311+
$this->assertSame('no', $view->children[1]->vars['label']);
312+
}
313+
277314
protected function getTestOptions(): array
278315
{
279316
return ['class' => Suit::class];

0 commit comments

Comments
 (0)