- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
过去我渲染一个表单,但现在我在模板中渲染表单的每个元素。
如何渲染 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/
使用底层实体中定义的常量数组填充 Symfony 表单 choiceType 并添加基于相同常量创建断言的最佳方法是什么。选项名称应存储在数据库中。 我正在使用 Symfony 2.8,这就是我所做的
我试图在 Symfony 3.0.9 中定义一些 ChoiceType 表单,其中填充了 ajax,具体取决于动态创建的选项数量,但随后验证表单显示所选选项无效。 我这样定义 ChoiceType:
我的 Symfony 4 应用程序中有一个表单。我有 2 个字段,一个 ChoiceType 和一个 TextType。如果在 ChoiceType 中选择了“其他”选项,是否可以启用 TextTyp
在 Symfony 2.8 上遇到一个小问题。我有几个数据库字段,一个是整数,一个是小数。当我构建表单时,这些字段是下拉列表,因此我使用 ChoiceType 而不是 IntegerType 或 Nu
我想用 ChoiceType 构建一个表单,选项值/选择基于数据库表(已经有记录)。 当表单显示时,宗教列表将在下拉列表/组合框中可用。 示例: $builder->add('name', Cho
Symfony 2.8.2 根据 Symfony 文档“选择选项是一个数组,其中数组键是项目的标签,数组值是项目的值” http://symfony.com/doc/2.8/reference/for
我在 Symfony 3.1.6 上运行,我在表单渲染中遇到了一些奇怪的问题。 这是我的表格: $form = $this->createFormBuilder() ->add
我想用 ChoiceType 构建一个表单,选项值/选择基于数据库表(已经有记录)。 当表单显示时,宗教列表将在下拉列表/组合框中可用。 示例: $builder->add('name', Cho
我目前正在将应用程序转换为 Symfony 3.3,我正在绞尽脑汁想知道如何才能实现以下目标。 我有一个 ChoiceType 字段,称为 Responsible(String 值),它是从数据库 V
我刚从 NoSQL 迁移到 SQL,对 SqlAlchemy ORM 还是个新手。 在我的用例中,我需要模型中的一个字段来存储给定的选择集: # models.py from sqlalchemy.e
我正在尝试构建一个我将在 Twig 模板中实现的表单。为此,我使用了一些 HTML 元素。其中之一是来自 Symfony 组件的 ChoiceType。我创建了一个传递给 add 方法的数组。我的愿望
在我的项目中,我有一些带有 选择类型的表单很多的选项。 所以我决定建立一个基于jquery自动完成的自动完成选择类型,它增加了新的原 HTML 元素 在运行时。选择后,它们会正确提交,但无法在默认 C
使用 Symfony 3.3,我已经尝试过类似的操作,但没有响应: {% for choice in form.vars.value.clients %} {{ choice
过去我渲染一个表单,但现在我在模板中渲染表单的每个元素。 如何渲染 ChoiceType 的每个项目以根据需要添加 div 和类。 我需要将所有选项与两个在一个 div 中成对分组 1 2 以及
我有一个多个EntityType,它从数据库获取他的值,如下所示: MyFormType.php private $membersRepository; public function __con
我想在数据库中保存一个整数值并将其显示为一种颜色,但我找不到正确的方法。 我的表格看起来像这样: public function buildForm(FormBuilderInterface $bui
我的项目要求我重建大型系统的功能,同时保留数据库结构(出于历史计费原因)。我从 Symfony 2.0.x 开始,并开始升级到 Symfony 2.1.2。 以前,我能够使用点表示法在 Twig 中直
我的表单类型有问题。我有一个实体事件和另一个实体类。它在 ManyToMany 中。当我显示表单时,它位于 ChoiceType 中,但我希望它位于 CheckboxType 中。所以我: publi
我有一个 Flask 模型: class User(db.Model): ROLE_USER = 0 ROLE_MODERATOR = 1 ROLE_ADMIN = 2
我正在使用 FosUserBundle 运行 Symfony 3.4。 我定制了注册表以获得Roles包括 : add('roles', ChoiceType::class, array(
我是一名优秀的程序员,十分优秀!