我发现你可以通过在twig.yaml文件中添加这个来告诉symfony以引导方式生成表单
$this->user = $token->getUser();
# ↓ BECOMES ↓
$this->user = $this->om->getReference(User::class, $token->getUser()->getId());
这会以正常的引导方式呈现复选框,如下所示:
twig:
form_themes: ['bootstrap_4_layout.html.twig']
这很棒,但我想以这种方式将其生成为自定义表单(我使用bootswatch模板):
<fieldset class="form-group">
<legend class="col-form-label required">Subjects</legend>
<div id="form_subjects">
<div class="form-check">
<input type="checkbox" id="form_subjects_0" name="form[subjects][]" class="form-check-input" value="1">
<label class="form-check-label" for="form_subjects_0">4IZ110</label>
</div>
<div class="form-check">
<input type="checkbox" id="form_subjects_1" name="form[subjects][]" class="form-check-input" value="2">
<label class="form-check-label" for="form_subjects_1">4MA106</label>
</div>
</div>
</fieldset>
我在symfony文档中发现,您可以将表单生成为自定义&#34;,但这只是基于将<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1" checked="">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>
</div>
添加到元素class="checkbox-custom"
所以我的问题是:有没有办法本地生成自定义引导复选框(和单选按钮)?或者我是否必须重写<div id="form_subjects">
?
答案 0 :(得分:1)
好的,当我仔细观察bootstrap_4_layout.html.twig
时,我找到了解决方案:
只需在创建表单时将'label_attr' => ['class' => 'checkbox-custom']
添加到选项中。
现在我的表单看起来像这样,它可以正常工作:
$form = $this->createFormBuilder()
->add(
'subjects',
ChoiceType::class,
[
'choices' => $this->subjectFacade->getAll(),
'choice_label' => 'indent',
'choice_value' => 'id',
'expanded' => true,
'multiple' => true,
'label_attr' => ['class' => 'checkbox-custom'],
]
)
->getForm();