Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] With form builder, auto-detect Enum properties #61272

@Nayte91

Description

@Nayte91

Description

Hello,

When you use FormBuilder to construct your Form Type, service can auto detect a bunch of properties, in a lot of Properties:

$builder
    ->add('description')

In this example, it knows $description is a text area type, with "description" label, ...

but for now, when you bind a Enum field, FormBuilder needs some pretty obvious settings that we could preshot by default?

Example

Assuming this kind of Value object:

class Organization
{
    public ?string $name = null;
    public ?OrganizationNature $nature = OrganizationNature::CLUB;
    public ?string $description = null;
    public Visibility $visibility = Visibility::Public;
}

This current example...

$builder
    ->add('name')
    ->add('nature', EnumType::class, [
        'class' => OrganizationNature::class,
        'required' => false,
        'choice_label' => 'value',
        'placeholder' => 'Nothing noticeable',
    ])
    ->add('description')
    ->add('visibility', EnumType::class, ['class' => Visibility::class])

...Could become:

$builder
    ->add('name')
    ->add('nature', options: [
        'required' => false, // Maybe because property is nullable, this is already set?
        'choice_label' => 'value',
        'placeholder' => 'Nothing noticeable',
    ])
    ->add('description')
    ->add('visibility')

Symfony 7.3: when I try to keep only ->add('visibility'), it throws an error :

ReflectionObject::__construct(): Argument #1 ($object) must be of type object, null given)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions