@@ -51,17 +51,31 @@ these values as ``<input type="checkbox">`` or ``<input type="radio">``.
51
51
52
52
The label displayed in the ``<option> `` elements of the ``<select> `` is the enum
53
53
name. PHP defines some strict rules for these names (e.g. they can't contain
54
- dots or spaces). If you need more flexibility for these labels, use the
55
- `` choice_label `` option and define a function that returns the custom label ::
54
+ dots or spaces). If you need more flexibility for these labels, your enum can
55
+ implements `` TranslatableInterface `` to translate or display custom labels ::
56
56
57
- ->add('textAlign', EnumType::class, [
58
- 'class' => TextAlign::class,
59
- 'choice_label' => fn ($choice) => match ($choice) {
60
- TextAlign::Left => 'text_align.left.label',
61
- TextAlign::Center => 'text_align.center.label',
62
- TextAlign::Right => 'text_align.right.label',
63
- },
64
- ]);
57
+ // src/Config/TextAlign.php
58
+ namespace App\Config;
59
+
60
+ enum TextAlign: string implements TranslatableInterface
61
+ {
62
+ case Left = 'Left aligned';
63
+ case Center = 'Center aligned';
64
+ case Right = 'Right aligned';
65
+
66
+ public function trans(TranslatorInterface $translator, string $locale = null): string
67
+ {
68
+ // Translate enum from name (Left, Center or Right)
69
+ return $translator->trans($this->name, locale: $locale);
70
+
71
+ // Translate enum using custom labels
72
+ return match ($this) {
73
+ self::Left => $translator->trans('text_align.left.label', locale: $locale),
74
+ self::Center => $translator->trans('text_align.center.label', locale: $locale),
75
+ self::Right => $translator->trans('text_align.right.label', locale: $locale),
76
+ };
77
+ }
78
+ }
65
79
66
80
Field Options
67
81
-------------
0 commit comments