将变量注入扩展树枝 form_theme [英] Inject variable into extended twig form_theme

查看:28
本文介绍了将变量注入扩展树枝 form_theme的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个动态表单,我想扩展一个小部件以将其显示为 .事实上,表格的每一列都是一个包含一些字段的动态组.

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.

我想要做的就是像这样迭代 myform_widget 中的组:

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

这是表头

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

这是 myform 块:

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

我得到一个异常变量组"不存在.我假设 form_theme 无法访问 groups 变量,但是我如何才能访问它?强文本谢谢o/

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/

推荐答案

我找到了一个简单的解决方案.

I have found a simple solution.

在我的表单类型文件中,我添加了一个 groups 属性,如下所示:

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

现在我可以使用:

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

这篇关于将变量注入扩展树枝 form_theme的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆