如何自定义 CollectionType() 项?
Symfony2 文档的 § /a> 展示了一种用于自定义单个字段的出色技术。我经常使用它,但有一个特定的 Type
我很难进行自定义:CollectionType()
自定义集合本身是相当困难的很简单,您可以执行以下操作:
{% block _mynamespace_mybundle_mytype_mycollectionfield_row %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_widget(prototype) }) %}
{% endif %}
{% spaceless %}
<ul {{ block('widget_container_attributes') }}>
{% spaceless %}
{{ form_errors(form) }}
{% for child in form %}
<li>
{{form_widget(child)}}
</li>
{% endfor %}
{% endspaceless %}
{{ form_rest(form) }}
</ul>
{% endspaceless %}
{% endblock %}
但是如何自定义集合的每个元素?如何使用Twig自定义data-prototype
(data-prototype
是一个特殊的属性,用于使用js添加新项目)?
我尝试做这样的事情(对于 data-prototype
):
{% block _mynamespace_mybundle_mytype_mycollectionfield_$$name$$_row %}
customization ok!
{% endblock %}
但是我收到错误,因为我不知道如何转义 $
关于这些项目,我尝试了很多事情:
{% block _mynamespace_mybundle_mytype_mycollectionfield_item_subfield_row %}
customization ok!
{% endblock %}
{% block _mynamespace_mybundle_mytype_mycollectionfield_element_subfield_row %}
customization ok!
{% endblock %}
它们都不起作用。
This § of the Symfony2 documentation shows an awesome technique for customizing an individual field. I'm using it a lot but there is one particular Type
for which I'm having a hard time doing the customization : the CollectionType()
Customizing the collection itself is quite easy, you can do something like this:
{% block _mynamespace_mybundle_mytype_mycollectionfield_row %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_widget(prototype) }) %}
{% endif %}
{% spaceless %}
<ul {{ block('widget_container_attributes') }}>
{% spaceless %}
{{ form_errors(form) }}
{% for child in form %}
<li>
{{form_widget(child)}}
</li>
{% endfor %}
{% endspaceless %}
{{ form_rest(form) }}
</ul>
{% endspaceless %}
{% endblock %}
But how can you customize each element of the collection ? And how can you customize the data-prototype
using Twig (the data-prototype
is a special attribute used to add new items with js)?
I tried doing something like this (for the data-prototype
):
{% block _mynamespace_mybundle_mytype_mycollectionfield_$name$_row %}
customization ok!
{% endblock %}
But I get errors, because I don't know how to escape the $
Regarding the items, I tried many things:
{% block _mynamespace_mybundle_mytype_mycollectionfield_item_subfield_row %}
customization ok!
{% endblock %}
{% block _mynamespace_mybundle_mytype_mycollectionfield_element_subfield_row %}
customization ok!
{% endblock %}
None of them work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 symfony 的错误跟踪器中提出了这个问题,似乎没有解决方案,但一个好的解决方法是为您想要自定义的每个子字段创建一个自定义类型。请参阅此问题
I asked the question in symfony's bug tracker, and it seems there is no solution, but a good workaround is creating a custom type for each subfield you want to customize. See this issue