Schyzophrenic
Schyzophrenic

Reputation: 131

Implement CollectionField with Autocomplete in EasyAdmin 3

In EasyAdmin 3 (I currently use 3.1.2), I am trying to implement a CollectionField, which will allow to reference a Collection of other entities.

The idea is to create a GameTop, which references other Game.

The entity of the Crud is GameTop and I add the CollectionField with:

$games = CollectionField::new('games')
            ->allowAdd()
            ->allowDelete()
            ->setEntryType(GameTopEntryType::class)
            ->showEntryLabel(false)
        ; 

The GameTopEntryType class is as below. I created this to be able to reference the Game as an EntityField, but I don't know if this is the right approach.

class GameTopEntryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('game', EntityType::class, [
                'class' => Game::class,
                'label' => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Game::class,
        ]);
    }
}

The form looks OK, but when adding games to a GameTop, the following error is triggered:

Neither the property "game" nor one of the methods "getGame()", "game()", "isGame()", "hasGame()", "__get()" exist and have public access in class "App\Entity\Game".

I must not be implementing the CollectionField properly, does anyone know what I am doing wrong (or use the autocomplete field from EasyAdmin inside a collection)?

Thank you

Upvotes: 2

Views: 3490

Answers (2)

SemNicholas
SemNicholas

Reputation: 31

Just add autocomplete attribute, and it will work

example:

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('game', EntityType::class, [
            'class' => Game::class,
            'label' => false,
            'attr' => ['data-ea-widget' => 'ea-autocomplete']
        ])
    ;
}

Upvotes: 3

Schyzophrenic
Schyzophrenic

Reputation: 131

With the version 3.4.1 of EasyAdmin, we can simply do:

$games = AssociationField::new('games')
            ->setCrudController(GameCrudController::class)->autocomplete();

This will allow multiple entries to be added. It is important to note this solution does not sort the elements though.

Upvotes: 0

Related Questions