3

I discovered that you can tell symfony to generate forms in bootstrap way, by adding this in your twig.yaml file

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

this renders renders checkbox in normal bootstrap way like this:

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

this it awesome, but I'd like to generate it as custom forms in this fashion (I'm using bootswatch template):

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

I've found in symfony documentation that you can "generate forms as custom", but that was just based upon adding class="checkbox-custom" to element <div id="form_subjects">

So my question is: Is there a way to generate custom bootstrap checkboxes (and radio buttons) natively? Or do I have to rewrite bootstrap_4_layout.html.twig?

1 Answer 1

9

ok, When I looked closer at the bootstrap_4_layout.html.twig, I found the solution: just add 'label_attr' => ['class' => 'checkbox-custom'] to options when creating form.

Now my form looks like this, and it works correctly:

$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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.