我正在尝试在我的小项目中自定义嵌入式表单原型。我已经在symfony.com上读到了关于如何做到这一点的简短描述,但目前我只成功了50%。在按照symfony的文档中描述的小部件定义之后,现有记录上的单行可以使用我的模板正确呈现。但是prototype仍然是默认的。
小部件定义如下:
{% form_theme form _self %}
{% block _refuel_form_fuels_entry_row %}
{% spaceless %}
<table class="form-table compact">
<tbody>
<tr>
<th>{{ form_label(form.fuel) }}</th>
<td>
{{ form_errors(form.fuel) }}
{{ form_widget(form.fuel) }}
</td>
<th>{{ form_label(form.currencyRatio) }}</th>
<td>
{{ form_errors(form.currencyRatio) }}
{{ form_widget(form.currencyRatio) }}
</td>
</tr>
... other rows ...
</tbody>
</table>
{% endspaceless %}
{% endblock %}使用此代码,我将为现有记录呈现行(编辑表单):
<tr>
<th>{{ form_label(form.fuels) }}</th>
<td colspan="3">
<ul class="fuels collection-holder" data-prototype="{{ form_widget(form.fuels.vars.prototype)|e('html_attr') }}">
{% for fuel in form.fuels %}
<li>
{{ form_row(fuel) }}
</li>
{% endfor %}
</ul>
</td>
</tr>所以,symfony知道我想为每个子表单使用自定义模板,但为什么它仍然不使用它来生成原型?我必须以某种方式启用该小部件才能与prototype一起工作吗?文档中没有这样的信息。
发布于 2017-07-24 03:03:05
不要紧,在发布问题后几分钟就意识到了问题所在。我的模板“覆盖”了与for循环中使用的form_row函数相对应的_row块。但在prototype属性中,我从使用form_widget函数的文档中复制了代码...看到问题了吗?是啊。将属性更改为:data-prototype="{{ form_row(form.fuels.vars.prototype)|e('html_attr') }}"和boom!原型生成正确。愚蠢的我..。
https://stackoverflow.com/questions/45268795
复制相似问题