14

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
  • 1
    Here's a link to a Symfony issue at Github that has some nice code to get folks headed in the right direction: Issue 3836 Commented Jan 3, 2014 at 7:04

5 Answers 5

31

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) }}
Sign up to request clarification or add additional context in comments.

2 Comments

I suggest using {% set entity = projectForm.stages.vars.choices[loop.index - 1].data %} instead of getting index from entity property value. This way you can avoid unknown array key error.
@NikDenisov the loop has also a loop.index0 variable: twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable so no need for -1
10

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.

6 Comments

For an entity form type I had to do this: {{ u.data.field }} when iterating through choices
How this got votes, I have no idea. It's just plain wrong. I posted a comment under the OP with a link to a solution.
@keyboardSmasher: you could notice that bernhard is the same person whom closed the issue that you've linked onto github so I'm sure that you can figure out, now, why this answer has 6 upvotes: I'm sure (as I would answer the same way) that this method is working
Is there a way to accomplish this with using a query_builder?
answer by Aaron below helped my scenario. Thanks anyway.
|
9

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

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

1 Comment

Hello, how can I do the same but with a CollectionType field ? Impossible to access to collection data because the method is private...
5

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 -%}

Comments

3

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 %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.