Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

twig

symfony

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

You can access values of the parent parent from a widget block using form.parent.vars


For example, we want to render the value from a type text field called primerNombre we will need

{{ form.vars.value.primerNombre }}

If we wanted to render the name of one of the children we will need

{% for hijo in form.hijos %}
    <td><div align="left">{{ form_widget(hijo.vars.value.primerNombre) }}</div></td>
{% endfor %}

Good luck!


In Symfony > 3 you may use:

form.vars.value.Entity.someValue