@@ -22,10 +22,7 @@ $ composer require yokai/enum-bundle
2222
2323``` php
2424<?php
25- // config/bundles.php
26-
2725return [
28- // ...
2926 Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],
3027];
3128```
@@ -43,7 +40,9 @@ We first need to create the classes that will handle our enums :
4340
4441``` php
4542<?php
46- // src/Enum/GenderEnum.php
43+
44+ declare(strict_types=1);
45+
4746namespace App\Enum;
4847
4948use Yokai\EnumBundle\EnumInterface;
@@ -60,16 +59,18 @@ class GenderEnum implements EnumInterface
6059}
6160```
6261
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.
6564
6665That's it, now the bundle know your enum services. You can start using it.
6766
68- Adding validation to your model :
67+ Add validation to any model :
6968
7069``` php
7170<?php
72- // src/Model/Member.php
71+
72+ declare(strict_types=1);
73+
7374namespace App\Model;
7475
7576use App\Enum\GenderEnum;
@@ -78,42 +79,44 @@ use Yokai\EnumBundle\Validator\Constraints\Enum;
7879class Member
7980{
8081 /**
81- * @var string
82- *
8382 * @Enum(GenderEnum::class)
8483 */
85- protected $gender;
84+ public ?string $gender = null ;
8685}
8786```
8887
89- Adding enum form types :
88+ Add enumerated form fields to any form :
9089
9190``` php
9291<?php
93- // src/Form/Type/MemberType.php
92+
93+ declare(strict_types=1);
94+
9495namespace App\Form\Type;
9596
96- use App\Enum\GenderEnum ;
97+ use App\Model\Member ;
9798use Symfony\Component\Form\AbstractType;
9899use Symfony\Component\Form\FormBuilderInterface;
99- use Yokai\EnumBundle\Form\Type\EnumType ;
100+ use Symfony\Component\OptionsResolver\OptionsResolver ;
100101
101102class MemberType extends AbstractType
102103{
103- public function buildForm(FormBuilderInterface $builder, array $options)
104+ public function buildForm(FormBuilderInterface $builder, array $options): void
104105 {
105106 $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 )
107108 ->add('gender')
108-
109- // Manual form type binding
110- ->add('gender', EnumType::class, ['enum' => GenderEnum::class])
111109 ;
112110 }
111+
112+ public function configureOptions(OptionsResolver $resolver): void
113+ {
114+ $resolver->setDefault('data_class', Member::class);
115+ }
113116}
114117```
115118
116- Displaying the label for an enum value within a template :
119+ Display label of any enum value within a Twig template :
117120
118121``` twig
119122{{ value|enum_label('App\\Enum\\GenderEnum') }}
0 commit comments