@@ -38,9 +38,9 @@ short) defined somewhere in your application. This enum has to be of type
3838
3939 enum TextAlign: string
4040 {
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';
4444 }
4545
4646Instead 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
5656the ``TextAlign `` enum. Use the `expanded `_ and `multiple `_ options to display
5757these values as ``<input type="checkbox"> `` or ``<input type="radio"> ``.
5858
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::
8563
8664 ->add('textAlign', EnumType::class, [
8765 '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',
9070 },
91- ])
71+ ]);
9272
9373Field Options
9474-------------
0 commit comments