表格树枝定制风格

时间:2016-05-16 14:22:11

标签: symfony-forms symfony

我在自定义表单样式时遇到问题。 我以这种正常方式扩展表单小部件:

{{ form_label(form.tipi) }}
{{ form_errors(form.tipi) }}
{{ form_widget(form.tipi) }}

但是我收到了这个糟糕的HTML:

<div id="requests_tipi" class="form-control scroll-select">
     <input type="checkbox" id="requests_tipi_1" name="requests[tipi][]" value="1" checked="checked">
     <label for="requests_tipi_1">Appartamento</label>
     <input type="checkbox" id="requests_tipi_2" name="requests[tipi][]" value="2" checked="checked">
     <label for="requests_tipi_2">Casa Colonica</label>
     <input type="checkbox" id="requests_tipi_3" name="requests[tipi][]" value="3" checked="checked">
     <label for="requests_tipi_3">Garage</label>
     <input type="checkbox" id="requests_tipi_4" name="requests[tipi][]" value="4">
     <label for="requests_tipi_4">Loft</label>
 </div>

我会这样列出:

<ul>
   <li><label for="requests_tipi_1">Appartamento</label><input type="checkbox" id="requests_tipi_1" name="requests[tipi][]" value="1" checked="checked"></li>
 ...
</ul>

如何自定义我的Symfony表格???

由于

1 个答案:

答案 0 :(得分:1)

这是您自定义单个字段(在您的情况下为tipi)的呈现方式:

http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field

这就是定义原始格式的地方(以及你可以在哪里获取“灵感”:):

https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L45

所以,像这样的东西可以工作(请注意我没有测试,可能需要调整),只需将以下代码放在模板文件顶部的某处:

{% form_theme form _self %}

{% block _requests_tipi_widget %}
    <ul {{ block('widget_container_attributes') }}>
    {% for child in form %}
        <li>
        {{ form_label(child, null, {translation_domain: choice_translation_domain}) }}
        {{ form_widget(child) }}
        </li>
    {% endfor %}
    </ul>
{% endblock _requests_tipi_widget %}