gpt4 book ai didi

symfony - 如何渲染 ChoiceType 的每个项目以根据需要添加 div 和类

转载 作者:行者123 更新时间:2023-12-02 13:28:48 25 4
gpt4 key购买 nike

过去我渲染一个表单,但现在我在模板中渲染表单的每个元素。

如何渲染 ChoiceType 的每个项目以根据需要添加 div 和类。

我需要将所有选项与两个在一个 div 中成对分组 <div class="col-xs-2"> <label class="checkbox"><input type="checkbox" class="styler" checked=""> 1</label> <label class="checkbox"><input type="checkbox" class="styler"> 2</label> </div>以及所有选项。

我需要将 Symfony 3 代码(形成 ChoiceType 类型的 elemetn)从:

<div class="col-xs-12">
<div class="form-group">
<label class="col-xs-2 text-right control-label">{% trans %}Example{% endtrans %}</label>
<div class="col-xs-2">
<label class="checkbox">{{ form_widget(form.examples, {'attr': {'class': 'form-control styler', 'checked': ''}}) }} Example</label>
</div>
</div>
</div>

该表格:

<div class="col-xs-12">
<div class="form-group">
<label class="col-xs-2 text-right control-label">{% trans %}Example{% endtrans %}</label>
<div class="col-xs-2">
<label class="checkbox"><input type="checkbox" class="styler" checked=""> 1</label>
<label class="checkbox"><input type="checkbox" class="styler"> 2</label>
</div>
<div class="col-xs-3">
<label class="checkbox"><input type="checkbox" class="styler"> 3</label>
<label class="checkbox"><input type="checkbox" class="styler" checked=""> 4</label>
</div>
<div class="col-xs-3">
<label class="checkbox"><input type="checkbox" class="styler" checked=""> 5</label>
<label class="checkbox"><input type="checkbox" class="styler"> 6</label>
</div>
</div>
</div>

我是 Symfony 的新手。谢谢您的帮助。

最佳答案

假设您有以下表单:

class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('inputName', ChoiceType::class, array(
'label' => 'Choice',
'choices' => [
'Choice 1'=>'c1',
'Choice 2'=>'c2',
'Choice 3'=>'c3',
'Choice 4'=>'c4',
'Choice 5'=>'c5',
'Choice 6'=>'c6',
]
));
}

public function getName()
{
return 'my_form';
}

}

然后您可以访问表单中的选项并将其分组,如下所示:

{{ form_widget(form) }}

{% set groupSize = 2 %}
{% set auxGroup = 0 %}
<div>
{% for choice in form.inputName.vars.choices %}
{{ dump(choice) }}
{% set auxGroup = auxGroup + 1 %}
{% if auxGroup == groupSize %}
</div><div>
{% set auxGroup = 0 %}
{% endif %}
{% endfor %}
</div>

我没有添加类、标签和其他前端内容,因为它们不相关。

希望对你有帮助!

关于symfony - 如何渲染 ChoiceType 的每个项目以根据需要添加 div 和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507745/

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