Skip to content

Better document EnumType with multiple #16594

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy