Mustapha GHLISSI
Mustapha GHLISSI

Reputation: 1730

Easyadmin: how to implement an autocomplete dynamic ChoiceField

I'm working on a project using Easyadmin

For a feature, I need to implement:

  1. A ChoiceField with autocompletion config
  2. This field options should be loaded based on an entity
  3. AssociationField doesn't work since I need a non-mapped field

Any help !

Upvotes: 0

Views: 76

Answers (1)

Mustapha GHLISSI
Mustapha GHLISSI

Reputation: 1730

After deep search and many tries, I got the right solution which is hooking into the autocomplete field natively and add some essential attributes.

Example: I have an entity called User and I'd like to have a ChoiceField with Users names List inside the HelloWorldCrudController:

$userUrl = $this->container->get(AdminUrlGenerator::class)
    ->set('autocompleteContext', array(
        'crudControllerFqcn' => HelloWorldCrudController::class,
        'propertyName' => 'garage',
        'originatingPage' => Action::INDEX,
    ))
    ->setController(UserCrudController::class)
    ->setAction('autocomplete')
    ->set('page', 1);


$userChoiceField = ChoiceField::new('user', 'User')
    ->setFormType(EntityType::class)
    ->setFormTypeOption('class', User::class)
    ->autocomplete()
    ->setFormTypeOption('attr.data-ea-autocomplete-endpoint-url', $userUrl)
   ;

Upvotes: 0

Related Questions