【问题标题】:Symfony form renderer - custom bootstrap checkbox and radio buttonsSymfony 表单渲染器 - 自定义引导复选框和单选按钮
【发布时间】:2018-04-11 07:50:57
【问题描述】:

我发现您可以告诉 symfony 以引导方式生成表单,方法是将其添加到您的 twig.yaml 文件中

twig:
   form_themes: ['bootstrap_4_layout.html.twig']

这会以正常的引导方式渲染复选框,如下所示:

<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>

这太棒了,但我想以这种方式将其生成为自定义表单(我正在使用 bootswatch 模板):

<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>

我在 symfony 文档中发现您可以“生成自定义表单”,但这只是基于将 class="checkbox-custom" 添加到元素 &lt;div id="form_subjects"&gt;

所以我的问题是:有没有办法在本地生成自定义引导复选框(和单选按钮)?还是我必须重写bootstrap_4_layout.html.twig?

【问题讨论】:

    标签: symfony twig bootstrap-4


    【解决方案1】:

    好的,当我仔细查看bootstrap_4_layout.html.twig 时,我找到了解决方案: 只需在创建表单时将'label_attr' =&gt; ['class' =&gt; '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();
    

    【讨论】:

      猜你喜欢
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多