Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bootstrap 5 Form Theme

Bootstrap 5 Form Theme

Edit this page

Symfony provides several ways of integrating Bootstrap into your application. The most straightforward way is to add the required <link> and <script> elements in your templates (usually you only include them in the main layout template which other templates extend from):

1
2
3
4
5
6
7
8
9
{# templates/base.html.twig #}

{# beware that the blocks in your template may be named different #}
{% block stylesheets %}
    <!-- Copy CSS from https://getbootstrap.com/docs/5.0/getting-started/introduction/#css -->
{% endblock %}
{% block javascripts %}
    <!-- Copy JavaScript from https://getbootstrap.com/docs/5.0/getting-started/introduction/#js -->
{% endblock %}

If your application uses modern front-end practices, it's better to use Webpack Encore and follow this tutorial to import Bootstrap's sources into your SCSS and JavaScript files.

The next step is to configure the Symfony application to use Bootstrap 5 styles when rendering forms. If you want to apply them to all forms, define this configuration:

1
2
3
# config/packages/twig.yaml
twig:
    form_themes: ['bootstrap_5_layout.html.twig']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- config/packages/twig.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:twig="http://symfony.com/schema/dic/twig"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/twig
        https://symfony.com/schema/dic/twig/twig-1.0.xsd">

    <twig:config>
        <twig:form-theme>bootstrap_5_layout.html.twig</twig:form-theme>
        <!-- ... -->
    </twig:config>
</container>
1
2
3
4
5
6
7
8
// config/packages/twig.php
use Symfony\Config\TwigConfig;

return static function(TwigConfig $twig): void {
    $twig->formThemes(['bootstrap_5_layout.html.twig']);

    // ...
};

If you prefer to apply the Bootstrap styles on a form to form basis, include the form_theme tag in the templates where those forms are used:

1
2
3
4
5
6
7
8
{# ... #}
{# this tag only applies to the forms defined in this template #}
{% form_theme form 'bootstrap_5_layout.html.twig' %}

{% block body %}
    <h1>User Sign Up:</h1>
    {{ form(form) }}
{% endblock %}

Note

By default, all inputs are rendered with the mb-3 class on their container. If you override the row_attr class option, the mb-3 will be overridden too and you will need to explicitly add it.

Error Messages

Unlike in the Bootstrap 4 theme, errors are rendered after the input element. However, this still makes a strong connection between the error and its <input>, as required by the WCAG 2.0 standard.

Checkboxes and Radios

For a checkbox/radio field, calling form_label() doesn't render anything. Due to Bootstrap internals, the label is already rendered by form_widget().

Inline Checkboxes and Radios

If you want to render your checkbox or radio fields inline, you can add the checkbox-inline or radio-inline class (depending on your Symfony Form type or ChoiceType configuration) to the label class.

1
2
3
4
5
6
7
8
9
10
11
$builder
    ->add('myCheckbox', CheckboxType::class, [
        'label_attr' => [
            'class' => 'checkbox-inline',
        ],
    ])
    ->add('myRadio', RadioType::class, [
        'label_attr' => [
            'class' => 'radio-inline',
        ],
    ]);
1
2
3
4
5
6
7
8
9
10
11
{{ form_row(form.myCheckbox, {
    label_attr: {
        class: 'checkbox-inline'
    }
}) }}

{{ form_row(form.myRadio, {
    label_attr: {
        class: 'radio-inline'
    }
}) }}

Switches

Bootstrap 5 allows to render checkboxes as switches. You can enable this feature on your Symfony Form CheckboxType by adding the checkbox-switch class to the label:

1
2
3
4
5
$builder->add('myCheckbox', CheckboxType::class, [
    'label_attr' => [
        'class' => 'checkbox-switch',
    ],
]);
1
2
3
4
5
{{ form_row(form.myCheckbox, {
    label_attr: {
        class: 'checkbox-switch'
    }
}) }}

Tip

You can also render your switches inline by simply adding the checkbox-inline class on the label_attr option:

1
2
3
4
5
// ...
'label_attr' => [
    'class' => 'checkbox-inline checkbox-switch',
],
// ...

Caution

Switches only work with checkbox.

Input group

To create input group in your Symfony Form, simply add the input-group class to the row_attr option.

1
2
3
4
5
6
$builder->add('email', EmailType::class, [
    'label' => '@',
    'row_attr' => [
        'class' => 'input-group',
    ],
]);
1
2
3
4
5
6
{{ form_row(form.email, {
    label: '@',
    row_attr: {
        class: 'input-group'
    }
}) }}

Caution

If you fill the help option of your form, it will also be rendered as part of the group.

Floating labels

To render an input field with a floating label, you must add a label, a placeholder and the form-floating class to the row_attr option of your form type.

1
2
3
4
5
6
7
8
9
$builder->add('name', TextType::class, [
    'label' => 'Name',
    'attr' => [
        'placeholder' => 'Name',
    ],
    'row_attr' => [
        'class' => 'form-floating',
    ],
]);
1
2
3
4
5
6
7
8
9
{{ form_row(form.name, {
    label: 'Name',
    attr: {
        placeholder: 'Name'
    },
    row_attr: {
        class: 'form-floating'
    }
}) }}

Caution

You must provide a label and a placeholder to make floating labels work properly.

Accessibility

The Bootstrap 5 framework has done a good job making it accessible for functional variations like impaired vision and cognitive ability. Symfony has taken this one step further to make sure the form theme complies with the WCAG 2.0 standard.

This does not mean that your entire website automatically complies with the full standard, but it does mean that you have come far in your work to create a design for all users.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Check Code Performance in Dev, Test, Staging & Production

    Check Code Performance in Dev, Test, Staging & Production

    Online Symfony certification, take it now!

    Online Symfony certification, take it now!

    Version:

    Table of Contents

    • Error Messages
    • Checkboxes and Radios
      • Inline Checkboxes and Radios
      • Switches
    • Input group
    • Floating labels
    • Accessibility

    Symfony footer

    Avatar of Yassine Guedidi, a Symfony contributor

    Thanks Yassine Guedidi (@yguedidi) for being a Symfony contributor

    11 commits • 641 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony