Symfony2 form collection: Index of the current obj

2020-02-17 04:58发布

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

标签: forms symfony
5条回答
放我归山
2楼-- · 2020-02-17 05:37

Using @MrBandersnatch's solution below, I had to use $view->vars['name'] instead of $this->vars['name'] (Symfony 2.3).

(apologies for not adding this as a comment on @MrBandersnatch's answer, I've not got enough reputation yet).

查看更多
Ridiculous、
3楼-- · 2020-02-17 05:41

You can custom the rendering of your collection for don't display the index with, by example:

{% block _FORMNAME_person_widget %}
{% spaceless %}
    {% for child in form %}
        {{ form_widget(child.Name) }}
        {{ form_widget(child.Address) }}
    {% endfor %}
{% endspaceless %}
{% endblock %}
查看更多
乱世女痞
4楼-- · 2020-02-17 05:47

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

查看更多
够拽才男人
5楼-- · 2020-02-17 05:48

I know this has been closed for a while. And not sure if this has been solved elsewhere. This issue is actually pretty simple to fix and I am surprised there is no documentation about this anywhere. In the PersonType or any type that is used in a collections just modify the vars['name'] in the buildView to be what you want displayed as the label.

public function buildView(FormView $view, FormInterface $form, array $options)
{
    // Adjust the view based on data passed
    $this->vars['name'] = $form->getData();
    // Or...
    $this->vars['name'] = 'Some random string';
}

If you want it dynamic, you would use the object by form->getData(). Since, in my problem, I am using a form theme, overriding the twig is not really an option for me.

Hope this helps someone.

查看更多
三岁会撩人
6楼-- · 2020-02-17 05:51

This one is some days ago but because I was facing same question for Symfony 3 the answer of sectus is the correct one.

Use the

'entry_options' => ['label'=>false],

option within your builder to hide he object item.

Best Regards

查看更多
登录 后发表回答