gpt4 book ai didi

forms - Symfony2 选择形式 split 成 Twig

转载 作者:行者123 更新时间:2023-12-04 17:28:31 27 4
gpt4 key购买 nike

好的,所以我有一个包含 2 个选项的选择表

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

我想使用 Twig 在 View 中拆分选项以获得如下所示的内容:
{{ form_widget(form.type/choice_1/) }}
some html stuf
{{ form_widget(form.type/choice_2/) }}

有什么建议吗?

最佳答案

您需要将模板添加到表单中。这是文档:
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 %}

你可以做任何你想做的事,只要离开: {{ form_widget(child, {'attr': {'class': attr.widget_class|default('')}}) }}独自的 :)
  • 您需要创建文件 YourBundle/Resources/Form/fields.html.twig
  • 粘贴上面的代码。
  • 向表单添加主题:{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
  • 准备好摇滚吧!
  • 关于forms - Symfony2 选择形式 split 成 Twig ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12507957/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com