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

后端 未结 5 847
清酒与你
清酒与你 2020-12-13 23:42

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         


        
相关标签:
5条回答
  • 2020-12-13 23:47

    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 }}
    
    0 讨论(0)
  • 2020-12-13 23:52

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

    0 讨论(0)
  • 2020-12-14 00:00

    In Symfony > 3 you may use:

    form.vars.value.Entity.someValue
    
    0 讨论(0)
  • 2020-12-14 00:04

    Edit2:

    Finally, I was indeed getting the value of the current row in {{ value }} here:

    {% 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="{{ value }}">Aperçu</a>
    {% endif %}
    
    {% endspaceless %}
    {% endblock field_widget %}
    

    But in my case I get a token instead of the value since I am using input type file. This is due to a security measure in Symfony2.

    0 讨论(0)
  • 2020-12-14 00:10

    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!

    0 讨论(0)
提交回复
热议问题