Skip to content

[Form] With a CheckboxType, form_label is empty and form_widget renders form_row #26540

Closed
@LucileDT

Description

@LucileDT
Q A
Bug report? yes
Feature request? no
BC Break report? maybe
RFC? no
Symfony version 3.4.6

When rendering a CheckboxType form input in a view, instead of separating label, errors and widget, Symfony renders the whole row of the input.

TestType.php

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', EmailType::class, [
            'label' => 'Email',
            'required' => true,
        ])
        ->add('emailIsPublic', CheckboxType::class, [
            'label' => 'Make e-mail public (can be seen by everyone)',
            'required' => false,
            'attr' => [
                'class' => 'switch',
            ]
        ])
        ->add('submit', SubmitType::class, [
            'label' => 'Save changes',
            'attr' => [
                'class' => 'btn btn-outline-primary float-right',
            ]
        ]);
}

test-view.html.twig

{{ form_start(edit_form) }}
    <div>
        {{ form_label(edit_form.emailIsPublic) }}
    </div>
    <div>
        {{ form_errors(edit_form.emailIsPublic) }}
    </div>
    <div>
        {{ form_widget(edit_form.emailIsPublic) }}
    </div>
{{ form_end(edit_form, {'render_rest': false}) }}

Generated HTML

<form name="appbundle_my" method="post">
    <div></div>
    <div></div>
    <div>
        <div class="form-check">
            <label class="form-check-label">
                <input id="appbundle_my_emailIsPublic" name="appbundle_my[emailIsPublic]" class="switch form-check-input" value="1" type="checkbox"> Make e-mail public (can be seen by everyone)
            </label>
        </div>
    </div>
</form>

As we can see in the generated HTML, form_label is empty and form_widget renders what form_row should render.

I can't find if it's an expected behaviour.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions