Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 form collection: Index of the current object is shown

Tags:

forms

symfony

I've got a problem with displaying collection in my form.

When displaying my entity collection I've got something like this :

0 Name: myInputName Address: myInputAddress  1 Name: myInputName Address: myInputAddress 

My question is why Symfony2 display the index...

And this for all saved entities into my collection...

Here the code I use:

$builder                     ->add('person', 'collection', array(                'label' => ' ',                          'type' => new PersonType(),             'prototype' => true,             'allow_add' => true,             'allow_delete' => true,             'by_reference' => false,         ))     ; 

In my twig file:

<div>     {{ form_widget(edit_form) }}     </div> 

Help please

Sam

like image 976
Sam Avatar asked Jan 18 '12 13:01

Sam


1 Answers

Removing indexes (labels) for collection items:

$builder                 ->add('person', 'collection', array(         ...         'options' => array('label' => false)     )) ; 

Use key entry_options instead of options for Symfony 3 and 4

If you want to add custom labels per row you can produce the form yourself:

{{ form_start(edit_form) }}     {% for person in form.persons %}         {{ form_row(person, {'label': 'custom label per item' }) }}     {% endfor %} {{ form_end(edit_form) }} 

Note: tested on Symfony 2.3 & 2.4

like image 186
Dovydas Bartkevičius Avatar answered Oct 04 '22 00:10

Dovydas Bartkevičius