Symfony EasyAdmin: complex forms

Nenad M.
2 min readAug 3, 2019

--

I first came upon EasyAdminBundle a while back, mid 2015. Courtesy of a colleague that was looking to save time by using a CMS generator other than Sensio’s generated CRUD. The guy went on to torpedo that and another project and we parted ways in a not-so-pleasant way. Just goes to show things are rarely black and white. He gave me a lot of headache, but left me with a tool I love and still eagerly use: EasyAdminBundle.

While it’s documentation is really, really good (especially compared to other CMS generators for Symfony), there are some things you’ll bang your head on. One of those things is how to show nested objects in a single form.

For example, you’d like to have a Doctrine embeddable object for street address, embed it into your Product class, and show it all in a single form.

Ideally, you’d be able to just slap “billingAddress.line1", “billingAddress.line2” etc into the config file, but EasyAdmin doesn’t yet support nested properties. So, the following config won’t get you anywhere:

The solution is to create a Symfony form class, that you’ll be able to add into the config. While it won’t look as pretty (not great, not terrible :D), the end user won’t see anything odd in the CMS.

Step 1: create a form for the embedded class

So, on to the first step: create the form for StreetAddress class. If you have symfony/maker-bundle, you can just type into the console:

$ ./bin/console make:form StreetAddressType StreetAddress

or manually create the class. Either way, this is what mine looks like:

Step 2: configure EasyAdmin to show your form

That config should produce a form looking like this:

And that’s it, you’re done! :)

The whole example project can be seen here: https://github.com/shonezlo/easy-admin-demo-complex-forms

--

--