EasyAdmin 3 : Nested forms (Callection in another Collection)

651 views Asked by At

I use EasyAdmin 3.

I'm creating a newsletter manager with Symfony 5. My entities are built as such.

  • Newsletter (ManyToOne => NewsletterItem)
    • NewsletterItem (ManyToOne => NewsletteItemSub)
      • NewsletteItemSub

In EasyAdmin, I was able to add a NewsletterItem in Newsletter. But I don't know how to add a NewsletterItemSub in NewsletterItem in EasyAdmin.

Does anyone have an idea?

My current code :

CollectionField::new('newsletterItems', 'Newsletter Items')
     ->allowAdd()
     ->allowAdd()
     ->setEntryIsComplex(false)
     ->setEntryType(NewsletterItemType::class)
     ->showEntryLabel(false)
     ->setFormTypeOptions([
         by_reference' => false
        ]
     )
     ->hideOnIndex(),

What I have now.

1

There are 1 answers

1
Flash On BEST ANSWER

Create NewsletteItemSubType form class and embed it in NewsletterItemType

Something like this:

<?php
#...
class NewsletterItemType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        #...

        $builder->add('tags', CollectionType::class, [
            'entry_type' => NewsletteItemSubType::class,
            'entry_options' => ['label' => false],
        ]);
    }

    #...
}

https://symfony.com/doc/current/form/form_collections.html