Directly access a form field's value when overriding widget in a twig template

Rishi picture Rishi · Sep 19, 2012 · Viewed 96.3k times · Source

What I want to do is get variables stored in form view.

{% form_theme edit_form _self %}

{% block field_widget %}
{% spaceless %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>

{# MY CODE #}
{% if type == "file" %}
<a class="BOpreview" href="{# NEED TO REPLACE VAR HERE #}">Aperçu</a>
{% endif %}

{# MY ATTEMPT #}
{{ form.title.get('value') }}
{{ form.vars.value.url }}

{% endspaceless %}
{% endblock field_widget %}

My form has properties like url, title, etc and I am trying to access them here to use it in the field widget block.

I searched for it and came on https://groups.google.com/forum/?fromgroups=#!topic/symfony2/onor9uFte9E that suggested:

{{ form.title.get('value') }}
{{ form.vars.value.url }}

which didn't work for me.

Note: If I do a var_dump on $form->createView() in my controller, I get:

    object(Symfony\Component\Form\FormView)[331]
    private 'vars' => 
      array (size=15)
        'value' => 
          object(Panasonic\TestEtAvisBundle\Entity\Product)[168]
            protected 'reviewArray' => 
              object(Doctrine\ORM\PersistentCollection)[234]
                ...
            protected 'testArray' => 
              object(Doctrine\ORM\PersistentCollection)[221]
                ...
            protected 'fbshareArray' => 
              object(Doctrine\ORM\PersistentCollection)[317]
                ...
            private 'id' => int 2
            private 'name' => string 'Nom du produit' (length=14)
            private 'title' => string '<span>Titre </span>' (length=19)
            private 'image' => string 'bundles/testetavis/uploads/product/0d9d9550.png' (length=47)
            private 'fbImage' => string 'bundles/testetavis/uploads/product/facebook//product_e928cd96.jpg' (length=65)
            private 'description' => string '<span>Descriptif </span>' (length=24)
            private 'url' => string 'http://www.google.com' (length=21)
            private 'creationDate' => 
              object(DateTime)[210]
                ...
            private 'modificationDate' => 
              object(DateTime)[209]
                ...
            private 'isDeleted' => int 0
        'attr' => 
          array (size=0)
            empty
        'form' => 
          &object(Symfony\Component\Form\FormView)[331]
        'id' => string 'panasonic_testetavisbundle_producttype' (length=38)
        'name' => string 'panasonic_testetavisbundle_producttype' (length=38)
        'full_name' => string 'panasonic_testetavisbundle_producttype' (length=38)

I want to access that url for instance but can't seem to be able to do it after many variations. Including use of {{ value }}, {{ value.url }} But inspite of vars, I can do {{ full_name }} and get panasonic_testetavisbundle_producttype.

Any ideas?

Edit2: I found out the real problem...

Edit3: Seeing that this question is quite popular I decided to clarify on what I attempted to do in case it helps someone in the same situation. If you rely strictly on what the question asks, as I stated from my research and that Besnik supported are indeed correct. Now what I wanted to do is for every input type file, get url from object used to render form and append a preview link, using retrieved url, beside the input type file. If you try to get the form var of an input type "file" like this "{{ form.vars.value.url }}" in my code, this doesn't work since, if I recall correctly, you receive a token instead of the url stored inside the object.

Answer

Besnik picture Besnik · Sep 19, 2012

You can access the current data of your form via form.vars.value:

{{ form.vars.value.title }}

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

Dump vars by using dump function:

{{ dump(form.vars.value) }}

If you are using subforms or want to have a value of a specific field:

{{ form.FIELD.vars.VALUE }}