我有一个动态表单要创建,我希望扩展一个小部件,以便将其显示为<tr>
。事实上,表的每一列都是包含某些字段的动态组。
我所要做的就是像这样迭代myform_widget
中的组:
这是表头
<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块:
{% 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 %}
我得到了一个例外Variable "groups" does not exist
。我假设form_theme不能访问groups
变量,但是我如何才能访问它呢?
发布于 2015-06-10 14:35:54
我找到了一个简单的解决办法。
在表单类型文件中,我添加了一个groups
attr,如下所示:
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 %}
https://stackoverflow.com/questions/30757770
复制相似问题