How to pass entity atribute value to form Symfony2

2019-02-20 16:54发布

I have a form type:

$builder->add('paises', 'collection', array(
                'options' => array('data_class' =>'Acc\ApssBundle\Entity\Pais'),
                'prototype' => true,
                'type' => new PaisType(),
                ))  ;

How to add attribute "name" to the class Pais to the label form ??

In twig I have:

{%  for pais in form.paises %}

<col>   

    <td>{{ form_label(pais.name)  }}</td>


</col>

{% endfor %}    

Thank you very much!!

1条回答
霸刀☆藐视天下
2楼-- · 2019-02-20 17:35

Quick and dirty:

It's not a solution but you could just as well do: <td>{{ pais.vars.data.name }}</td>.

Proper way:

Within PaisType setup the FormEvent so form is data-aware:

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
    $pais = $event->getData();
    $form = $event->getForm();

    # Use concreate data tp construct form field
    $form->add('some_custom_field_with_custom_label', 'text', array('label' => $pais->getName()));
});

Hope either of these help ;)

查看更多
登录 后发表回答