PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] EasyAdmin 3 : Nested forms (Callection in another Collection)

 January 16, 2022     easyadmin3, php, symfony     No comments   

Issue

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.


Solution

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



Answered By - Flash
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing