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):
{% block stylesheets %}
{% endblock %}
{% block javascripts %}
{% 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:
<?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>
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:
{% form_theme form 'bootstrap_5_layout.html.twig' %}
{% block body %}
<h1>User Sign Up:</h1>
{{ form(form) }}
{% endblock %}
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.
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
.
For a checkbox/radio field, calling
form_label()
doesn't render anything.
Due to Bootstrap internals, the label is already rendered by
form_widget()
.
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.
->
add(
'myCheckbox'
, CheckboxType
::
class, [
'label_attr'
=> [
'class'
=>
'checkbox-inline'
,
->
add(
'myRadio'
, RadioType
::
class, [
'label_attr'
=> [
'class'
=>
'radio-inline'
,
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:
$builder->add('myCheckbox', CheckboxType::class, [
'label_attr' => [
'class' => 'checkbox-switch',
You can also render your switches inline by simply adding the
checkbox-inline class on the label_attr option:
'label_attr' => [
'class' => 'checkbox-inline checkbox-switch',
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.
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.
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