Bootstrap 5 Form Theme
Warning: You are browsing the documentation for Symfony 7.0, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
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']
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',
],
]);
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',
],
]);
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',
],
]);
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',
],
]);
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.