Overriding symfony radio widget

社会主义新天地 提交于 2019-12-07 15:15:33

问题


Default radio widget creates a following structure:

<label>...</label>
<div id="...">
<div class="clearfix prettyradio labelright  blue">
<input type="radio" id="..._0" name="..." value="..." style="display: none;">
<a href="#" class=""></a>
...
</div>

I found the radio_widget block, but it contains only an input itself. So I can customize there only this part:

<input type="radio" id="..._0" name="..." value="1" style="display: none;">

But I can't understand how to change whole the structure of radio choice field?

Also, does anybody knows, why symfony adds display:none to the input?

Thanks.


回答1:


if you're using Radio Field Type, you can customize only the input part of the radio_widget block by calling form_widget(form.yourField), all it displays is,

{% block radio_widget %}
{% spaceless %}
    <input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{% endspaceless %}
{% endblock radio_widget %}

But if you're using Choice Field Type to display Radio Fields (expanded => true and multiple => false). You'll then have to override the choice_widget block, which call for each child element the radio_widget block surrounded by a global div

How did you get the "display:none"? because there's no style such this in the default block.




回答2:


If you specifically want to override the way an individual radio field is rendered - i.e. how each input field in the group is rendered - use this formula for the block name:

_<form name>_<field name>_entry_widget

Note this bit: _entry

If you're using an expanded choice field ..._entry_row and ..._entry_label won't work because they aren't used for the individual choices - well for radio buttons at least.

More generally you can find out a lot about which block Symfony intends to use during the next call to {{ form_widget(form) }} function call using this code:

{% for b in form.vars.block_prefixes %}
    {{ dump(b) }}
{% endfor %}

or you might want to look at child in some situations:

{% for b in child.vars.block_prefixes %}
    {{ dump(b) }}
{% endfor %}


来源:https://stackoverflow.com/questions/13807409/overriding-symfony-radio-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!