2 years ago

#13795

test-img

Micka Bup

Add tag in a list with CollectionField in EasyAdmin

My tags and User entities are in ManyToMany's relation. In easyAdmin (edit page) I want to add a tag from a list so this is how I did in my UserCrudController :

 yield CollectionField::new('tags', 'Ajouter')

With this, I have my input but I can't choose a tag from the list, I just have a blank input : enter image description here

Then I created a TagType :

class TagType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('title', EntityType::class, [
                'class' => Tag::class
            ])
        ;
    }

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

And in my UserCrudController I added in CollectionField the EntryType:

yield CollectionField::new('tags', 'Ajouter')
                    ->setEntryType(TagType::class)

This time I could choose a tag from the list but if I click on edit, I have an error which tell me : An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type' cannot be null So I guess Symfony is trying to create a new tags but I only want to add in the user's collection the tag.
Any idea ?

php

symfony

easyadmin

0 Answers

Your Answer

Accepted video resources