Inject variable into extended twig form_theme

284 views Asked by At

I have a dynamic form to create and i want to extend a widget in order to display it as <tr>. The fact is that each column of the table is a dynamic group containing some fields.

All I want to do is iterate over groups in the myform_widget like that :

Here is the table header

<table id="myforms-table" class="table">
    <thead>
    <tr>
        {% for group in groups %}<th>{{ group.label }}</th>{% endfor %}
    </tr>
    </thead>
    <tbody data-prototype="{{ form_widget(form.myforms.vars.prototype)|e }}">

    </tbody>
</table>

Here is the myform block :

{% block myform_widget %}
    <tr>

        {% for group in groups %}
            <td>
                {% for field in group.fields %}
                    {{ form_row( form.children.(field.codeField) ) }}
                {% endfor %}
            </td>
        {% endfor %}

    </tr>
{% endblock %}

And I get an exception Variable "groups" does not exist. I assume that the form_theme cannot access to groups variable, but how can I have access to it ? strong text Thanks o/

1

There are 1 answers

0
Senorihl On BEST ANSWER

I have found a simple solution.

In my form type file I've added a groups attr, like that :

public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->vars['groups'] =  $this->groups;
}

Now I'm able to retrieve it with :

{% block partition_widget %}
    <tr>
        {% for group in form.vars.groups %}
            <td>
                {% for field in group.fields %}
                    {{ form_row(  attribute(form, field.codeField) ) }}
                {% endfor %}
            </td>
        {% endfor %}
    </tr>
{% endblock %}