8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 43066ff commit 65f26daCopy full SHA for 65f26da
src/Symfony/Component/Form/Extension/Core/Type/EnumType.php
@@ -14,6 +14,7 @@
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\OptionsResolver\Options;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
+use Symfony\Contracts\Translation\TranslatableInterface;
18
19
/**
20
* A choice type for native PHP enums.
@@ -29,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void
29
30
->setAllowedTypes('class', 'string')
31
->setAllowedValues('class', enum_exists(...))
32
->setDefault('choices', static fn (Options $options): array => $options['class']::cases())
- ->setDefault('choice_label', static fn (\UnitEnum $choice): string => $choice->name)
33
+ ->setDefault('choice_label', static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name)
34
->setDefault('choice_value', static function (Options $options): ?\Closure {
35
if (!is_a($options['class'], \BackedEnum::class, true)) {
36
return null;
src/Symfony/Component/Form/Tests/Extension/Core/Type/EnumTypeTest.php
@@ -16,8 +16,11 @@
use Symfony\Component\Form\Tests\Fixtures\Answer;
use Symfony\Component\Form\Tests\Fixtures\Number;
use Symfony\Component\Form\Tests\Fixtures\Suit;
+use Symfony\Component\Form\Tests\Fixtures\TranslatableTextAlign;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
21
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
22
+use Symfony\Component\Translation\IdentityTranslator;
23
24
25
class EnumTypeTest extends BaseTypeTestCase
26
{
@@ -257,6 +260,20 @@ public function testChoiceLabel()
257
260
$this->assertSame('Yes', $view->children[0]->vars['label']);
258
261
}
259
262
263
+ public function testChoiceLabelTranslatable()
264
+ {
265
+ $form = $this->factory->create($this->getTestedType(), null, [
266
+ 'multiple' => false,
267
+ 'expanded' => true,
268
+ 'class' => TranslatableTextAlign::class,
269
+ ]);
270
+
271
+ $view = $form->createView();
272
273
+ $this->assertInstanceOf(TranslatableInterface::class, $view->children[0]->vars['label']);
274
+ $this->assertEquals('Left', $view->children[0]->vars['label']->trans(new IdentityTranslator()));
275
+ }
276
277
protected function getTestOptions(): array
278
279
return ['class' => Suit::class];
src/Symfony/Component/Form/Tests/Fixtures/TranslatableTextAlign.php
@@ -0,0 +1,27 @@
1
+<?php
2
3
+/*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
12
+namespace Symfony\Component\Form\Tests\Fixtures;
13
+use Symfony\Contracts\Translation\TranslatorInterface;
+enum TranslatableTextAlign implements TranslatableInterface
+{
+ case Left;
+ case Center;
+ case Right;
+ public function trans(TranslatorInterface $translator, string $locale = null): string
+ return $translator->trans($this->name, locale: $locale);
27
+}