13.
CollectionType Field
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
24 Comments
Hi everyone,
I'm stating now with EasyAdmin, and I'm struggling with collection type, I see that you use a FormType to build the Collection in entry_type. How do I tell it to use another admin? I want to use a collection of a entity mapped with the admin, do I really need to create de FormType for it?
Hey rafaelmansilha!
Sorry for my extra slow reply! Yea, I think I understand what you're asking. The answer is: maybe :). y You probably do "really need to create a FormType for it"... but avoiding it may be possible.
When you render a form page in EasyAdmin, the form class is actually this one - https://github.com/EasyCorp/EasyAdminBundle/blob/1.x/src/Form/Type/EasyAdminFormType.php - it's a super-dynamic form type that builds itself based on the entity and your admin options. So, in theory, you could create a form with this, pass in whatever entity you want with an "entity" option, and it will build itself. And then you could embed it. It would look something like this inside of your "main" form type:
Will that work? I have no idea :). But now you have me interested ;). Let me know if you try this and whether it works or not.
Cheers!
Hi !
I tried your method, but it missing the 'view' option in the 'entry_option'. What should we add in this field ?
Thank you in advance for your help !
Hey Edouard Pihet
By looking at this class https://github.com/EasyCorp... I understand that the view option contains the data you defined for such entity and view (create or edit). I find it difficult to extend so I suggest to create your own FormType, it would be cleaner and easier
Cheers!
Hi everyone,
I am having an issue with easyadmin. It is a kind of issue I saw in Ryan tutorials, but I never saw this issue resolved. Basically, when I edit an entry and one particular field is a ManyToOne relation, EasyAdmin (by default) generate an autocomplete dropdown field. Now, assuming I have two entities User and Product, when I try to edit a product, I can see who is the user owing that product. The issue is that inside that dropdown, I can only see the name of the user as oppose to name and surname. I can see that easyadmin calls getName from the User entity, but I don't know how to tell easyAdmin to call a different method from inside the User Entity.
Any idea?
Hey Alessandro D.
EasyAdmin fully relies on the Symfony Form component, so you can do the exact same thing EasyAdmin forms as you would do with "normal" forms. What you need to do is to specify the "choice_label" option (Docs: https://symfony.com/doc/current/reference/forms/types/entity.html#choice-label)
I hope it helps :)
Cheers!
Hi Diego,
I have tried what you suggested, but I get the following error:
"The option "choice_label" does not exist".
Here is my code:
`
`
If I change all the block for 'usedBy' from this to normal '-usedBy' I do get the dropdown, but only the name is displayed
Hey Alessandro D.
Oh, sorry my bad, instead of specifying
type: collection, useentityDocs about EntityType: https://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage
Then, you will be able to specify its
choice_labelattribute. Let me know if it worked :)Hi Diego,
That is great. It works like a charm.
Thanks a lot. I can't believe I actually spent an entire evening trying to figure that out. !!! :(
Thanks again.
Alessandro
I hear you man. Symfony Forms are complex but the documentation is great. Every time I'm in doubt I go to the docs and check for any option that may fit my needs.
Cheers!
Hi Team !
I faced a funny behaviour with embed form (collection).
So I have this in easy_admin.yaml
It works fine, except for the validation.
All fields are required, but there is no small red asterisk and the form can submit with all fields empty.
Even the html5 validation doesn't work.
Any clue about how to fix this ?
Here is the formType
Hey Kaizoku!
Hmm. So, it sounds like there are two things going on: the HTML5 validation doesn't work AND the server-side validation also does not work. Is that right? These sound like similar systems, but actually they are quite separate.
First, about the HTML 5 validation, I see that you have
'required' => truefor your "name" and "language" fields. If you look in the HTML for those fields, do you see arequiredattribute or not?Second, about the server-side validation, you may need to add the
@Validconstraint above thefilesproperty on your entity so that the validation applies to this sub-entity.Let me know if either of these things helps!
Cheers!
First of all thank you so much Ryan for your answer.
> do you see a required attribute or not?
nope ! So maybe this an issue with the easy_admin form theme file ?
> you may need to add the @Valid constraint above the files property on your entity
yep ! I added validation constraint in the entity and it's now working. At least there is doctrine exception no more.
Don't bother about the HTML5 (unless you know why) since there is form validation it's good enough.
Hey Kaizoku!
> nope ! So maybe this an issue with the easy_admin form theme file ?
Hmm, maybe - I'm not sure. I would look click on the form icon on the web debug toolbar (the clipboard icon), select one of the fields that should be required, and see if the "required" option and "required" view variables are indeed set correctly.
That will at least give you something to check out ;).
Cheers!
Hi,
Something cool in sonata is the ability to create a new entry directly from the form containing the relation:
By example: Posts have many tags. When I create a new Post, I select one or many tags. If a tag is missing I can create a new tag in a modal without leaving the post form.
Pretty easy to setup in sonata, is there a trick to do the same in easyAdmin ?
Hey Geoffrey M.!
Actually, this is also one of my favorite features of Sonata! Unfortunately, it's not something that exists in EasyAdminBundle :/. So, it would need to be added custom :/ - no easy way! If you have enough of these things, then using Sonata starts to make more sense.
Cheers!
Hi! I'm following your course for my project but I've an issue.
I've two entities "shop" and "pictures" OneToMany relationship, one shop many pictures. Easy!
I can manage "shop" and "pictures" entites fine from EasyAdminBundle, the problem is when I try to delete or add new photos from "shop" entity, nothing happens it's so weird.
The error is:
Expected value of type "AppBundle\Entity\Shop" for association field "AppBundle\Entity\Picture#$shop", got "string" instead.
Any ideas?
Thanks!
Hey Roberto S.
When you try to add a new photo to a shop, don't you get any error message ? try checking the logs, or if you click in the profiler bar, there is a section of "Requests", where you can check older requests, I believe this "add photo action" is handled via AJAX, maybe you could find out what's going on in there.
Cheers!
The error is:
Expected value of type "AppBundle\Entity\Shop" for association field "AppBundle\Entity\Picture#$shop", got "string" instead.
The request isn't via AJAX :(
Hey Roberto,
Hm, it's weird. Are you sure it's directly related to EasyAdminBundle? Please, make sure you don't call "$picture->setShop('some-string');" manually somewhere in your code. Also, could you show us your easy_admin configuration for Shop and Picture entities?
Cheers!
Thanks Victor, I found the problem, check above.
I've identified the problem.
My Entity "Picture" doesn't initialize the "Shop" field so I've changed the field "Shop" in my PictureEmbeddedForm to 'JavierEguiluz\Bundle\EasyAdminBundle\Form\Type\EasyAdminAutocompleteType' because I've more than 30.000 shops. But it doesn't work when I add new Pictures, only to update or delete.
How can I initialize the "Shop" field of "Picture" entity when I clic on "add new item" on the Collection?
I don't really need a select field for that, It's enough with a hidden field to save the relationship.
Thanks for your help.
Problem solved!!!
I forgot to add $foto->setShop($this); on addPicture method.
public function addPicture(Picture $picture {
$foto->setShop($this);
return $this->pictures->add($picture);
}
Thanks!
Ohh, so the problem was that you forgot to setup the relationship when adding a picture. Good job debugging the problem, the part of the error message "got string instead" confused me.
Cheers!
"Houston: no signs of life"
Start the conversation!