@@ -22,10 +22,7 @@ $ composer require yokai/enum-bundle
22
22
23
23
``` php
24
24
<?php
25
- // config/bundles.php
26
-
27
25
return [
28
- // ...
29
26
Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],
30
27
];
31
28
```
@@ -43,7 +40,9 @@ We first need to create the classes that will handle our enums :
43
40
44
41
``` php
45
42
<?php
46
- // src/Enum/GenderEnum.php
43
+
44
+ declare(strict_types=1);
45
+
47
46
namespace App\Enum;
48
47
49
48
use Yokai\EnumBundle\EnumInterface;
@@ -60,16 +59,18 @@ class GenderEnum implements EnumInterface
60
59
}
61
60
```
62
61
63
- If you are using [ PSR-4 service discovery] ( https://symfony.com/blog/new-in-symfony-3-3-psr-4-based-service-discovery ) ,
64
- then your service is already registered.
62
+ If you are using [ PSR-4 service discovery] ( https://symfony.com/blog/new-in-symfony-3-3-psr-4-based-service-discovery )
63
+ (or Symfony default services file), then your service is already registered.
65
64
66
65
That's it, now the bundle know your enum services. You can start using it.
67
66
68
- Adding validation to your model :
67
+ Add validation to any model :
69
68
70
69
``` php
71
70
<?php
72
- // src/Model/Member.php
71
+
72
+ declare(strict_types=1);
73
+
73
74
namespace App\Model;
74
75
75
76
use App\Enum\GenderEnum;
@@ -78,42 +79,44 @@ use Yokai\EnumBundle\Validator\Constraints\Enum;
78
79
class Member
79
80
{
80
81
/**
81
- * @var string
82
- *
83
82
* @Enum(GenderEnum::class)
84
83
*/
85
- protected $gender;
84
+ public ?string $gender = null ;
86
85
}
87
86
```
88
87
89
- Adding enum form types :
88
+ Add enumerated form fields to any form :
90
89
91
90
``` php
92
91
<?php
93
- // src/Form/Type/MemberType.php
92
+
93
+ declare(strict_types=1);
94
+
94
95
namespace App\Form\Type;
95
96
96
- use App\Enum\GenderEnum ;
97
+ use App\Model\Member ;
97
98
use Symfony\Component\Form\AbstractType;
98
99
use Symfony\Component\Form\FormBuilderInterface;
99
- use Yokai\EnumBundle\Form\Type\EnumType ;
100
+ use Symfony\Component\OptionsResolver\OptionsResolver ;
100
101
101
102
class MemberType extends AbstractType
102
103
{
103
- public function buildForm(FormBuilderInterface $builder, array $options)
104
+ public function buildForm(FormBuilderInterface $builder, array $options): void
104
105
{
105
106
$builder
106
- // Let the bundle guess the form type for you (requires that you configured the validation )
107
+ // The bundle will find out the form type for you (thanks to the Enum constraint we added to model )
107
108
->add('gender')
108
-
109
- // Manual form type binding
110
- ->add('gender', EnumType::class, ['enum' => GenderEnum::class])
111
109
;
112
110
}
111
+
112
+ public function configureOptions(OptionsResolver $resolver): void
113
+ {
114
+ $resolver->setDefault('data_class', Member::class);
115
+ }
113
116
}
114
117
```
115
118
116
- Displaying the label for an enum value within a template :
119
+ Display label of any enum value within a Twig template :
117
120
118
121
``` twig
119
122
{{ value|enum_label('App\\Enum\\GenderEnum') }}
0 commit comments