Let say that you already has such enum, from myclabs/php-enum.
<?php
declare(strict_types=1);
namespace App\Model;
use MyCLabs\Enum\Enum;
/**
* @method static self NEW
* @method static self VALIDATED
* @method static self DISABLED
*/
class MemberStatus extends Enum
{
private const NEW = 'new';
private const VALIDATED = 'validated';
private const DISABLED = 'disabled';
}
If you want to integrate with the bundle, you just have to declare an enum for that class.
<?php
declare(strict_types=1);
namespace App\Enum;
use App\Model\MemberStatus;
use Yokai\EnumBundle\MyCLabsEnum;
class StatusEnum extends MyCLabsEnum
{
public function __construct()
{
parent::__construct(MemberStatus::class);
}
}
Or if you want to translate enum constant labels.
<?php
declare(strict_types=1);
namespace App\Enum;
use App\Model\MemberStatus;
use Symfony\Contracts\Translation\TranslatorInterface;
use Yokai\EnumBundle\MyCLabsTranslatedEnum;
class StatusEnum extends MyCLabsTranslatedEnum
{
public function __construct(TranslatorInterface $translator)
{
parent::__construct(MemberStatus::class, $translator, 'status.%s');
}
}
Because values of enum like StatusEnum
above are objects, it is not possible to submit it via HTTP.
As described in the documentation Symfony will use an incrementing integer as value.
Example, with StatusEnum
above:
0
will be the value forMemberStatus::NEW
1
will be the value forMemberStatus::VALIDATED
2
will be the value forMemberStatus::DISABLED
But, if you do not like this behavior, you can configure the form to use values instead:
<?php
namespace App\Form\Type;
use App\Enum\StatusEnum;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Yokai\EnumBundle\Form\Type\EnumType;
class MemberType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('status', EnumType::class, [
'enum' => StatusEnum::class,
'enum_choice_value' => true,
]);
}
}
Now, still with StatusEnum
above:
"new"
will be the value forMemberStatus::NEW
"validated"
will be the value forMemberStatus::VALIDATED
"disabled"
will be the value forMemberStatus::DISABLED