Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the AssociationField choice label when using autocomplete() with EasyAdmin 3.x

Is it possible to use a property of the associated Entity as the choice label, rather than the associated Entity ID when using the autocomplete method on an AssociationField in an EasyAdmin EntityCrudController?

Example:

I have 3 Entities with the following properties:

User
  name

Certification
  name

UserCertification
  User
  Certification

I have the following in my UserCertificationCrudController:

public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('user')
                ->autocomplete(),
            AssociationField::new('memberCertification')
                ->autocomplete(),
            'lastRenewed',
            'expiration',
        ];
    }

See the following screenshot when creating a new UserCertification: The autocomplete value is a reference to the respective Entity ID. How would we replace User# with the name property on the user?

Things I have already tried with no success:

  • using a ChoiceField and supplying the 'class' and 'choice_label' to the ChoiceField->setFormTypeOptions() method, as well as the result of UserRepository->FindAll() to the ChoiceField->setChoices() method
  • supplying the 'class' and 'choice_label' to the AssociationField->setFormTypeOptions() method
like image 812
BrandynL Avatar asked Oct 13 '25 07:10

BrandynL


1 Answers

Do you try to mmake the __toString() method of the entity ?

like image 145
Ionik Avatar answered Oct 16 '25 08:10

Ionik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!