formssymfonytwigarraycollection

Data in array collection shows empty when retrieving info


I'm creating a web app with symfony. I'm currently building the forms and as I've never used the ManyToMany relation I'm having some problem with retrieving the information.

The form I'm working with is this one:

enter image description here

The point is that when I'm retrieving the information of the array in the twig template, the data property inside the array shows empty, when there is Alumne's entities created. Let me show you.

Twig template (create page):

enter image description here

As you can see, what I pretend is to retrieve the info of every Alumne entity inside the array.

What I get doing this is:

enter image description here

The alumnes field is completly empty. But if I change the form Builder to this:

enter image description here

(I have also changed a little bit the twig template to make it more readable) It works!

enter image description here

And as you can see there is an Alumne created.

The point is, as you can see, the select and option tag it creates is kinda ugly, I want to custom so it can fit the rest of the forms. So that's why I need a CollectionType in the builder and I don't know what I'm doing wrong. Also if I check the dump(form.alumnes.vars.data) it shows empty.


Solution

  • The issue here is, that you use the CollectionType instead of the EntityType. To load data from the Database into a form, you should use the EntityType https://symfony.com/doc/current/reference/forms/types/entity.html

    If you leave the type out, Symfony will try to guess what form element to use and will (correctly) guess the EntityType