Skip to content

Better document EnumType with multiple #16594

Closed
@ThomasLandauer

Description

@ThomasLandauer

@derrabus Am I right that connecting an EnumType form field with multiple to an entity is not straightforward?
Since you can't define an array of enums in the entity, the field looks something like this:

#[ORM\Column(type: Types::SIMPLE_ARRAY)]
private array $features = [];

But in order to hydrate the array items as enum (not as string), you need a DataTransformer on the form field, right?

What do you think about adding a full example at https://symfony.com/doc/current/reference/forms/types/enum.html#multiple ?

Due to the DataTransformer, the validation rule you suggested at symfony/symfony#43047 (comment) isn't working, so I'm guessing the only way to get validation on the entity is:

#[Assert\Choice(callback: [Features::class, 'values'], multiple: true)]

... with this in the enum:

public static function values(): array
{
    return array_column(self::cases(), 'value');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions