@@ -38,9 +38,9 @@ short) defined somewhere in your application. This enum has to be of type
38
38
39
39
enum TextAlign: string
40
40
{
41
- case Left = 'Left/Start aligned';
42
- case Center = 'Center/Middle aligned';
43
- case Right = 'Right/End aligned';
41
+ case Left = 'Left aligned';
42
+ case Center = 'Center aligned';
43
+ case Right = 'Right aligned';
44
44
}
45
45
46
46
Instead of using the values of the enumeration in a ``choices `` option, the
@@ -56,39 +56,19 @@ This will display a ``<select>`` tag with the three possible values defined in
56
56
the ``TextAlign `` enum. Use the `expanded `_ and `multiple `_ options to display
57
57
these values as ``<input type="checkbox"> `` or ``<input type="radio"> ``.
58
58
59
- Since the label displayed in the ``<select> `` options is the enum name, you might sometimes
60
- want more flexibility as PHP strongly restricts the usable characters for those.
61
- You could do this by implementing a function in your enum class which returns a label
62
- or even a translation string for each possible enum::
63
-
64
- // src/Config/TextAlign.php
65
- namespace App\Config;
66
-
67
- enum TextAlign: string
68
- {
69
- case Left = 'Left/Start aligned';
70
- case Center = 'Center/Middle aligned';
71
- case Right = 'Right/End aligned';
72
-
73
- public function label(): string
74
- {
75
- return match ($this) {
76
- self::Left => 'text_align.left.label',
77
- self::Center => 'text_align.center.label',
78
- self::Right => 'text_align.right.label',
79
- };
80
- }
81
- }
82
-
83
- You can then use the ``choice_label `` option of ``EnumType `` with a function that
84
- returns the label::
59
+ The label displayed in the ``<option> `` elements of the ``<select> `` is the enum
60
+ name. PHP defines some strict rules for these names (e.g. they can't contain
61
+ dots or spaces). If you need more flexibility for these labels, use the
62
+ ``choice_label `` option and define a function that returns the custom label::
85
63
86
64
->add('textAlign', EnumType::class, [
87
65
'class' => TextAlign::class,
88
- 'choice_label' => static function (TextAlign $choice): string {
89
- return $choice->label();
66
+ 'choice_label' => match ($choice) {
67
+ TextAlign::Left => 'text_align.left.label',
68
+ TextAlign::Center => 'text_align.center.label',
69
+ TextAlign::Right => 'text_align.right.label',
90
70
},
91
- ])
71
+ ]);
92
72
93
73
Field Options
94
74
-------------
0 commit comments