Symfony2 : accessing entity fields in Twig with an entity field type

我怕爱的太早我们不能终老 提交于 2019-12-03 11:51:10

问题


Here is my FormType :

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('user', 'entity', array(
            'class'   => 'UserBundle:User',
            'expanded' => true,
            'property' => 'name',
        ));
}

Is there a way to access user's fields in the view (Twig) ?

I'd like to do something like this :

{% for u in form.user %}
    {{ form_widget(u) }}
    {{ form_label(u) }}
    {% if u.moneyLeft > 0 %}
    <span>{{ u.name }} : {{ u.moneyLeft }} €</span>
    {% endif %}
{% endfor %}

... where moneyLeft and name are fields from User entity.


回答1:


In Symfony 2.5 - you can accomplish this by accessing the data from each choice using the child's index value.

In the form builder - as you might expect:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // Generate form
    $builder
        ->add('child', 'entity', array(
            'class'         => 'MyBundle:Child',
            'label'         => 'Children',
            'property'      => 'any_property_for_label',
            'expanded'      => true,
            'multiple'      => true
        ));
}

In the Twig template:

{{ form_start(form) }}
{% for child in form.child %}
    {% set index = child.vars.value %}{# get array index #}
    {% set entity = form.child.vars.choices[index].data %}{# get entity object #}
    <tr>
        <td>{{ form_widget(child) }}</td>{# render checkbox #}
        <td>{{ entity.name }}</td>
        <td>{{ entity.email }}</td>
        <td>{{ entity.otherProperty }}</td>
    </tr>
{% endfor %}
{{ form_end(form) }}



回答2:


As of today, you can do the following in the master branch (and upcoming 2.1):

{{ u.vars.data.name }}

u is the form view for the user, which contains a list of attached variables. The data variable contains the normalized data of the form, which usually is your object (unless you added a custom model transformer).

In earlier versions of Symfony, you can do:

{{ u.vars.value.name }}

The value variable contains the view data of the form, which is also your object (unless you added a custom model or view transformer).

If you are working on Symfony master or >=2.1, I recommend to access data instead of value.




回答3:


This worked for me in Symfony 3.1 for a radio widget:

{% set entity = form.parent.vars.choices[form.vars.name].data %}



回答4:


Version 2.6.7

Similar to what Aaron Geiser suggested, you can use customised form widgets to achieve this:

{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{%- block entity_widget -%}
    <div {{ block('widget_container_attributes') }}>
    {%- for n, child in form %}
        {{- form_widget(child, {
            'entity': form.vars.choices[n].data
        }) -}}
        {{- form_label(child) -}}
    {% endfor -%}
    </div>
{%- endblock %-}

{%- block radio_widget -%}
{# You now have access to entity #}
{%- endblock -%}



回答5:


Update of Bernhard Schussek's answer for Symfony 2.8.4 (or even somewhat lower version):

{% for key,value in form.user %} {# key is the ID in the database table #}
    {{ form_widget(value, {'label':value.vars.label}) }}
    {{ form.user.vars.choices[key].data.moneyLeft }} {# this syntax is new #}
{% endfor %}


来源:https://stackoverflow.com/questions/10335104/symfony2-accessing-entity-fields-in-twig-with-an-entity-field-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!