Easyadminbundle: Embedded edit form?

Created on 16 Feb 2017  路  8Comments  路  Source: EasyCorp/EasyAdminBundle

Apologies if this has been discussed before, but I've spent all week pouring through everything -- the doc/book, issues, EasyAdmin code, StackOverflow, etc. -- and I cannot figure out how to embed a form.

To use a simple example, say I have a User entity with the following fields:

  • username
  • password

There is a oneToOne association with Address entity, which has the following fields:

  • street1
  • street2
  • city
  • state
  • zip

When an end-user edits User, they need to be able to edit the Address fields. So the end result form should look like:

  • username
  • password
  • street1
  • street2
  • city
  • state
  • zip

It seems like a simple and basic use-case, but I cannot for the life of me figure out how to make this work. Any suggestions or help is greatly appreciated!

bug unconfirmed

Most helpful comment

Did you try something like this ?

easy_admin:    
    entities:
        User:
            class: XXX\Entity\User                
            form:
                fields:
                    - 'username'
                    - 'password'
                    - { property: 'address', type: 'XXX\Form\AdressType', type_options: { required: true } }   

All 8 comments

I think you have to create a complex/dynamic backend, customizing the AdminController and the twig templates.

https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/book/7-complex-dynamic-backends.md

I tried all of that, didn't work. Finally got it working, though it was by accident.

I was reading that you could create a custom form type and then in the EasyAdmin config, specify the type param for the field as the fully qualified name of the custom FormType. This would not work -- the Symfony FormRegistry would also throw an exception saying FormType now found.

However, I got it working by creating a UserAddressType that contained all of the address fields, then created a service for it with tag of form.type and alias 'user_addrsss'. From there, I specified the EasyAdmin type Parma as 'user_address' and it worked perfectly.

TL;DR: Using FQCN for custom form type does not work. It only worked when registering custom form type as a service and then referencing that alias in YAML config.

Did you try something like this ?

easy_admin:    
    entities:
        User:
            class: XXX\Entity\User                
            form:
                fields:
                    - 'username'
                    - 'password'
                    - { property: 'address', type: 'XXX\Form\AdressType', type_options: { required: true } }   

@CruzyCruz - Yes, that's exactly what I tried. I cannot load a custom form type if I specify the fully qualified class name. It only works if I setup the form type as a service and call it by it's named alias.

This works:
[in services.yml]
form.type.address_type: class: XXX\Form\AddressType tags: - { name: form.type, alias: address_type_service }

[in easy_admin_config.yml]
- { property: 'address', type: 'address_type_service', type_options: { required: true } }

But this does not:
[in easy_admin_config.yml]
- { property: 'address', type: 'XXX\Form\AddressType', type_options: { required: true } }

I triple-checked my class names, namespaces, had other developers check it. Everything checked out and looked correct, but it still wouldn't load custom form types when using the FQCN.

FYI I'm using EasyAdmin v1.16.5.

@jplammie

It's really strange, I don't understand why you have to declare the form type as a service in this case. Do you have others form type configured in EA that works without this workaround ?

@CruzyCruz - No, all of my custom form types in EasyAdmin have to be declared as services before they can be used. Elsewhere, in regular Symfony forms, I can instantiate my custom form types directly without invoking a service. It's only in EA where I have to setup services for the form types. Definitely strange behavior that I can't readily explain.

Could be "fieldType" instead of "type"?

I'm closing this issue because it's probably too old to be still relevant. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

javiereguiluz picture javiereguiluz  路  4Comments

seb-jean picture seb-jean  路  3Comments

haithem-rihane picture haithem-rihane  路  4Comments

tamert picture tamert  路  3Comments

joazvsoares picture joazvsoares  路  4Comments