Symfony Override Bootstrap Style for Radio Button

I am building a form in Symfony 4 and I have multiple groups of radio inputs on my page and I need to replace the form-check form-check-inline classes that is placed around each radio input.

This is the output html:

<div class="form-check form-check-inline">        
    <input type="radio" id="feedback_answer_1_0" name="feedback[answer_1]" required="required" class="form-check-input" value="1">
    <label class="form-check-label required" for="feedback_answer_1_0">1</label>
</div>

This is my form type:

$builder->add('answer_1, ChoiceType::class, 'choices' => [
            1 => 1,
            2 => 2,
            3 => 3,
            4 => 4,
            5 => 5,
            6 => 6,
            7 => 7,
            8 => 8,
            9 => 9,
            10 => 10
        ],
        'label' => $i,
        'expanded' => true,
        'label_attr' => [
            'class' => 'radio-inline'
        ],
        'constraints' => [
            new NotBlank(),
        ]);

And this is my TWIG template:

     {{ form_start(form) }}
            {{ form_errors(form) }}
                {% for i in form.children %}
                    {% if i.vars.block_prefixes[1] != 'hidden' %}
                        <fieldset class="form-group">
                            <legend class="col-form-label required">{{ i.vars.label }}</legend>
                            {{ form_widget(i, {'attr': {'class': 'd-flex justify-content-between'}}) }}
                            <div>
                                {{ form_errors(i) }}
                            </div>
                        </fieldset>
                    {% endif %}
                {% endfor %}
        {{ form_end(form) }}

I have tried to override the form_widget function call using the solution offered here: Symfony Bootstrap 4 Form Layout Inheritance

But I get the error:

Block "radio_widget" should not call parent() in "form.html.twig" as the block does not exist in the parent template "base.html.twig".

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *