Skip to content

Commit 46b4de4

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: fix merge fix Relay Cluster 0.12 compatibility 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 fe2a58a + f35f67f commit 46b4de4

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
@@ -267,6 +267,43 @@ public function testChoiceLabelTranslatable()
267267
$this->assertEquals('Left', $view->children[0]->vars['label']->trans(new IdentityTranslator()));
268268
}
269269

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

0 commit comments

Comments
 (0)