Symfony2选择形式分裂在树枝上

Wol*_*bat 3 forms symfony-forms symfony twig

好的,所以我有一个有两个选项的选择表

$builder->add('type', 'choice', array(
    'label' => 'User type',
    'choices' => array('1' => 'Customer', '2' => 'Supplier'),
    'expanded' => true,
    'multiple' => false,
    'required' => false,
));
Run Code Online (Sandbox Code Playgroud)

我想使用twig在视图中拆分选项来得到这样的东西:

{{ form_widget(form.type/choice_1/) }}
some html stuf
{{ form_widget(form.type/choice_2/) }}
Run Code Online (Sandbox Code Playgroud)

任何sugestions?

Max*_*cki 9

您需要将模板添加到表单.这是文档:http: //symfony.com/doc/current/cookbook/form/form_customization.html

这里有多个例子:https: //github.com/phiamo/MopaBootstrapBundle/blob/master/Resources/views/Form/fields.html.twig

这个字段适合你:

{% block choice_widget_expanded %}
{% spaceless %}
<div {{ block('widget_container_attributes') }}>
{% for child in form %}
    <label class="{{ (multiple ? 'checkbox' : 'radio') ~ (widget_type ? ' ' ~ widget_type : '') ~ (inline is defined and inline ? ' inline' : '') }}">
        {{ form_widget(child, {'attr': {'class': attr.widget_class|default('')}}) }}
        {{ child.vars.label|trans({}, translation_domain) }}
    </label>
{% endfor %}
</div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
Run Code Online (Sandbox Code Playgroud)

你可以做任何你想做的事情只需离开:{{ form_widget(child, {'attr': {'class': attr.widget_class|default('')}}) }}独自:)

  1. 你需要创建文件 YourBundle/Resources/Form/fields.html.twig
  2. 粘贴上面的代码.
  3. 将主题添加到表单: {% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
  4. 并准备好摇滚!