自定义Symfony2中选择/实体字段的呈现 [英] Customize the rendering of a choice/entity field in Symfony2
问题描述
我希望在其< option>
上用其他数据呈现< select>
元素.举例来说,我想有一个服务选择器(非多个实体字段),该服务选择器会在选择更改时重置另一个输入值.我对使用JS数据结构不感兴趣,我需要使render字段看起来如下:
I would like a <select>
element to be rendered with additional data on its <option>
s. For the sake of example, I'd like to have a service-selector (non-multiple entity-field) that resets another inputs value upon selection change. I'm not interested in using JS data-structures, I need to have the rendered field to look as follows:
<select name="...">
<option value="1" data-price="90">Service 1</option>
<option value="2" data-price="40">Service 2</option>
</select>
我将采用两种不同的解决方案,并且很高兴看到它们的答案.
I would take two different solutions and would be glad to see the answer to both of them.
- 我将使用传递给树枝的
form
变量开始组装上述HTML代码,从而在Twig中手动呈现该字段.解决这个问题有两个问题. A),我找不到一种安全的方法来告诉文件名,即如何通过使用变量name
属性.> form.service (service是FormType中字段的名称).[请根据观察Symfony当前当前命名字段的方式,将一些值连接起来的技巧;我应该不依赖于接口而不是逆向工程.] B)我不知道如何访问选项列表,即由entity
字段的query_builder
选项.[因为我正在寻找一个通用的解决方案,所以我不愿意将这些项目复制到控制器的树枝参数中,只是为了避免此类建议.] - 我已经覆盖了相关字段块的呈现,如本食谱的表单样式一章中所建议的那样,但是存在三个问题. A)我无法确定应该覆盖哪些块(因此找不到示例). B),我会将参数从表单生成器传递给块,以使其知道要呈现哪些额外的
data-
属性,但我不知道该怎么做这.最后是 C),在我不需要偏离标准渲染的情况下(例如,当字段为多个时),我不知道如何退回到默认渲染.
- I'd render the field manually in Twig by starting to assemble the above HTML code by using the
form
variable I passed to the twig. I have two problems solving this. A) I can't find a safe way to tell what the filed should be named, i.e. how do I get thename
attribute that Symfony expects by using the variableform.service
(service is the name of the field in the FormType). [Please spare me tricks that concatenate some values based on observing how fields are currently named by Symfony; one should rely on interfaces and not on reverse engineering.] B) I don't know how to access the list of choices, i.e. the array assembled by theentity
field'squery_builder
option. [Since I'm looking for a general solution, I'm not willing to duplicate these items over to a twig-parameter in the controller -- just to avoid such suggestions.] - I'd override the rendering of the relevant field blocks, as suggested in the form styling chapter of the cookbook, but there are three problems with that. A) I cannot find out which blocks should be overridden (and so I don't find samples). B) I would pass parameter from the form builder to the block to let it know what extra
data-
attributes are to be rendered, but I don't know how to do this. And finally C) in those cases where I don't need to deviate from standard rendering (e.g. when the field is multiple) I don't know how to fallback to the default rendering.
所以实际上这是5个问题(1A,1B,2A,2B,2C),但我认为它们对其他回答在一起的人更有用,因为它们都解决了我认为是关于选择字段渲染的一个未记录的问题./p>
So these are actually 5 questions (1A,1B,2A,2B,2C) but I thought them to be more useful to others answered together, since they all address what I think is an undocumented spot regarding choice field rendering.
推荐答案
1..手动呈现.最好是使用单独的字段形式,而不重复执行某项形式,因为这样可以减少工作时间.
1. Manually rendering. Better if is a individual form for field and not repeated somethere as it requires less time to work.
A)使用 form.service.vars.full_name
B)选项列表- form.service.vars.choices
.它是 ChoiceView
的数组,要使实体只需访问公共 data
属性即可.
B) List of choices - form.service.vars.choices
. Its an array of ChoiceView
, to get entity simply access public data
property.
{% for choice in form.service.vars.choices %}
{% set service_entity = choice.data %}
{% endfor %}
2..通过覆盖模板.如果您想蛮力地拾取必须覆盖的块的名称.
2. Via overriding templates. IF you like to brute-force pick up the name of the blocks which must be overriden.
A)您只能将 widget
, label
和 errors
块替换为文档).
A) You can only override widget
, label
and errors
blocks as documentation said. You can specify block by widget name(documentation). Something like
{% block _form_service_widget %}
{% if expanded %}
{{ block('choice_widget_expanded') }}
{% else %}
{{ block('my_service_widget') }}
{% endif %}
{% endblock %}
{% block my_service_widget %}
{% spaceless %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value="">{{ empty_value|trans({}, translation_domain) }}</option>
{% endif %}
{% set options = choices %}
{{ block('my_service_options') }}
</select>
{% endspaceless %}
{% endblock my_service_widget %}
{% block my_service_options %}
{% spaceless %}
{% for group_label, choice in options %}
{# here you can access choice #}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
{% endfor %}
{% endspaceless %}
{% endblock my_service_options %}
这篇关于自定义Symfony2中选择/实体字段的呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!