Simple enumeration system with Symfony integration

Installs: 48 390

Dependents: 0

Suggesters: 0

Security: 0

Stars: 10

Watchers: 5

Forks: 4

Open Issues: 5


v3.3.0 2021-05-24 12:26 UTC


Latest Stable Version Latest Unstable Version Total Downloads License

This repository aims to provide simple enumeration implementation to Symfony.


Add the bundle as a dependency with Composer

$ composer require yokai/enum-bundle

Enable the bundle in the kernel

return [
    Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],


Let's take an example : our application has some members and each member has a gender which can be "male" (m) or "female" (f).

We first need to create the classes that will handle our enums :



namespace App\Enum;

use Yokai\EnumBundle\EnumInterface;
use Yokai\EnumBundle\EnumWithClassAsNameTrait;

class GenderEnum implements EnumInterface
    use EnumWithClassAsNameTrait;

    public function getChoices(): array
        return ['m' => 'Male', 'f' => 'Female'];

If you are using PSR-4 service discovery (or Symfony default services file), then your service is already registered.

That's it, now the bundle know your enum services. You can start using it.

Add validation to any model :



namespace App\Model;

use App\Enum\GenderEnum;
use Yokai\EnumBundle\Validator\Constraints\Enum;

class Member
     * @Enum(GenderEnum::class)
    public ?string $gender = null;

Add enumerated form fields to any form :



namespace App\Form\Type;

use App\Model\Member;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MemberType extends AbstractType
    public function buildForm(FormBuilderInterface $builder, array $options): void
            // The bundle will find out the form type for you (thanks to the Enum constraint we added to model)

    public function configureOptions(OptionsResolver $resolver): void
        $resolver->setDefault('data_class', Member::class);

Display label of any enum value within a Twig template :

{{ value|enum_label('App\\Enum\\GenderEnum') }}


MIT License

License can be found here.


The bundle was originally created by Yann Eugoné. See the list of contributors.

Thank's to Prestaconcept for supporting this bundle.