view raw
Rishi Rishi - 7 months ago 51
Twig Question

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

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

{{ 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!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:

private 'vars' =>
array (size=15)
'value' =>
protected 'reviewArray' =>
protected 'testArray' =>
protected 'fbshareArray' =>
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 '' (length=21)
private 'creationDate' =>
private 'modificationDate' =>
private 'isDeleted' => int 0
'attr' =>
array (size=0)
'form' =>
'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.


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

{{ form.vars.value.title }}

See Symfony2 Forms documentation:

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