|
| 1 | +# PHP native enum integration |
| 2 | + |
| 3 | +Let say that you already has such enum, from [PHP](https://www.php.net/manual/en/language.enumerations.php). |
| 4 | + |
| 5 | +```php |
| 6 | +<?php |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace App\Model; |
| 11 | + |
| 12 | +enum MemberStatus: string |
| 13 | +{ |
| 14 | + case NEW = 'new'; |
| 15 | + case VALIDATED = 'validated'; |
| 16 | + case DISABLED = 'disabled'; |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +> **Note** |
| 21 | +> Here, we are using a `StringBackedEnum`, but it is not required. |
| 22 | +> The bundle supports any form of `UnitEnum`, backed or not. |
| 23 | +> https://www.php.net/manual/en/language.enumerations.backed.php |
| 24 | +
|
| 25 | +## Standard enum |
| 26 | + |
| 27 | +If you want to integrate with the bundle, you just have to declare an enum for that class. |
| 28 | + |
| 29 | +```php |
| 30 | +<?php |
| 31 | + |
| 32 | +declare(strict_types=1); |
| 33 | + |
| 34 | +namespace App\Enum; |
| 35 | + |
| 36 | +use App\Model\MemberStatus; |
| 37 | +use Yokai\EnumBundle\NativeEnum; |
| 38 | + |
| 39 | +class StatusEnum extends NativeEnum |
| 40 | +{ |
| 41 | + public function __construct() |
| 42 | + { |
| 43 | + parent::__construct(MemberStatus::class); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Translated enum |
| 49 | + |
| 50 | +Or if you want to translate enum constant labels. |
| 51 | + |
| 52 | +```php |
| 53 | +<?php |
| 54 | + |
| 55 | +declare(strict_types=1); |
| 56 | + |
| 57 | +namespace App\Enum; |
| 58 | + |
| 59 | +use App\Model\MemberStatus; |
| 60 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 61 | +use Yokai\EnumBundle\NativeTranslatedEnum; |
| 62 | + |
| 63 | +class StatusEnum extends NativeTranslatedEnum |
| 64 | +{ |
| 65 | + public function __construct(TranslatorInterface $translator) |
| 66 | + { |
| 67 | + parent::__construct(MemberStatus::class, $translator, 'status.%s'); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Submitting values through a form |
| 73 | + |
| 74 | +Because values of enum like `StatusEnum` above are objects, it is not possible to submit it via HTTP. |
| 75 | +As described in the [documentation](https://symfony.com/doc/current/reference/forms/types/choice.html#choice-value) Symfony will use an incrementing integer as value. |
| 76 | +Example, with `StatusEnum` above: |
| 77 | +- `0` will be the value for `MemberStatus::NEW` |
| 78 | +- `1` will be the value for `MemberStatus::VALIDATED` |
| 79 | +- `2` will be the value for `MemberStatus::DISABLED` |
| 80 | + |
| 81 | +But, if you do not like this behavior, you can configure the form to use values instead: |
| 82 | +```php |
| 83 | +<?php |
| 84 | + |
| 85 | +namespace App\Form\Type; |
| 86 | + |
| 87 | +use App\Enum\StatusEnum; |
| 88 | +use Symfony\Component\Form\AbstractType; |
| 89 | +use Symfony\Component\Form\FormBuilderInterface; |
| 90 | +use Yokai\EnumBundle\Form\Type\EnumType; |
| 91 | + |
| 92 | +class MemberType extends AbstractType |
| 93 | +{ |
| 94 | + public function buildForm(FormBuilderInterface $builder, array $options): void |
| 95 | + { |
| 96 | + $builder->add('status', EnumType::class, [ |
| 97 | + 'enum' => StatusEnum::class, |
| 98 | + 'enum_choice_value' => true, |
| 99 | + ]); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Now, still with `StatusEnum` above: |
| 105 | +- `"new"` will be the value for `MemberStatus::NEW` |
| 106 | +- `"validated"` will be the value for `MemberStatus::VALIDATED` |
| 107 | +- `"disabled"` will be the value for `MemberStatus::DISABLED` |
0 commit comments