Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add empty value to AssociationField in EasyAdminBundle

I want my AssociationField to display a blank value by defaut, in ordre to force the users to select an item.

As I understand, AssociationField is based on EntityType FormType.

My Problem is, if the field of my entity is required, the create form renders the AssociationField without empty value, and thus selects the first value.

I haven't seen any option to add a placeholder to a select for required properties.

Here are some samples:

My Doctrine entity :

//Player.php
#[ORM\ManyToOne(inversedBy: 'players')]
#[ORM\JoinColumn(name:'team_id', referencedColumnName: 'id', nullable: false)]
private Team $team;

... and my CrudController

//PlayerCrudController.php
public function configureFields(string $pageName): iterable
{
    return [
        //...
        AssociationField::new('team'),
        //...
    ]
}

And the create form displays a select with already a value selected.

I've looked into the docs but couldn't find how to achieve this.

like image 900
NguyenK Avatar asked Sep 20 '25 13:09

NguyenK


1 Answers

Try ->setRequired(false), works for me with EasyAdmin 4.6.1.

like image 96
Johan Avatar answered Sep 22 '25 18:09

Johan