19
votes

I'm in a Twig template, and I have a "form" variable that represents a Doctrine2 Entity Form.

This Entity has properties that are mapped into the form, but the Entity has also some methods that I would like to access from my Twig template.

I would love to do something like this:

{{ form.myMethod }}

or maybe something like this:

{{ form.getEntity.myMethod }}

but unfortunately it doesn't work.

How could I achieve what I need?

8

8 Answers

34
votes

To access your entity from your FormView in a twig template you can use the following code

{{ form.get('value') }}

Where form is your FormView object. This will return your entity and from there you can call any methods on it. If you embed a collection of entities or a single entity in your form you can access it the same way

{{ form.someembedform.get('value') }}

or

{% for obj in form.mycollection %}
  {{ obj.get('value').someMethod }}
{% endif %}
18
votes

An even more convenient syntax to get the underlying entity instead of

{{ form.get('value') }}

is this:

{{ form.vars.value }}

Then you can call any entity method like this:

{{ form.vars.value.someMethod }}

See also the Form chapter in the Symfony2 Docs:

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

15
votes

Just in order to update the subject:

form.get('value')

is deprecated since symfony 2.1. Copy from Symfony\Component\Form\FormView :

/*
 * @deprecated Deprecated since version 2.1, to be removed in 2.3. Access
 *             the public property {@link vars} instead.
 */
public function get($name, $default = null) ....

so, I guess

form.vars.value.youMethod()

should be the way to go. It has worked form me.

... and there it goes my first post here. hope it helps!

6
votes

Lost few hours trying to figure out what's going on and why

{{ form.vars.value }}

is NULL.

If you have form.element (not the form object itself) object, for example if you are overriding a form_row template that has passed the form.row object, you can get the Entity like this:

{{ form.getParent().vars.value.MyEntityMethod }}

hope that helps someone!

EDIT: Year and so later - another useful tip:

{% block sonata_type_collection_widget %}
    {% for child in form %}
        {{ child.vars.form.vars.value.name }}
    {% endfor %}
{% endblock %}
3
votes

object methods should work in twig, I know I used them in some project.

try to use ()

like {{ form.myMethod() }}

1
votes

It seems that at some point the value is actually null. So you can use

{{ (form.vars.value != null) ? form.vars.value.yourEntityMethod():'' }}

tested in SF v3.0.6.

0
votes

None of the above worked for me in version 2.6.7. I used 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 %-}
-1
votes

use {{ form.getData.myMethod }}.