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

Source: Symfony Questions

Was this helpful?

1 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *