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 SensioLabs
  1. Home
  2. Documentation
  3. Bootstrap 4 Form Theme
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Accessibility
  • Custom Forms
  • Labels and Errors

Bootstrap 4 Form Theme

Edit this page

Warning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

Bootstrap 4 Form Theme

Symfony provides several ways of integrating Bootstrap into your application. The most straightforward way is to just 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 head_css %}
    <!-- Copy CSS from https://getbootstrap.com/docs/4.0/getting-started/introduction/#css -->
{% endblock %}
{% block head_js %}
    <!-- Copy JavaScript from https://getbootstrap.com/docs/4.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 4 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_4_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
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/twig
        http://symfony.com/schema/dic/twig/twig-1.0.xsd">

    <twig:config>
        <twig:form-theme>bootstrap_4_layout.html.twig</twig:form-theme>
        <!-- ... -->
    </twig:config>
</container>
1
2
3
4
5
6
7
8
// config/packages/twig.php
$container->loadFromExtension('twig', array(
    'form_themes' => array(
        'bootstrap_4_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_4_layout.html.twig' %}

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

Accessibility

The Bootstrap 4 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.

Custom Forms

Bootstrap 4 has a feature called "`custom forms`_". You can enable that on your Symfony Form RadioType and CheckboxType by adding a class called radio-custom and checkbox-custom respectively.

1
2
{{ form_row(form.myRadio, {label_attr: {class: 'radio-custom'} }) }}
{{ form_row(form.myCheckbox, {label_attr: {class: 'checkbox-custom'} }) }}

Labels and Errors

When you use the Bootstrap form themes and render the fields manually, calling form_label() for a checkbox/radio field doesn't render anything. Due to Bootstrap internals, the label is already rendered by form_widget().

Form errors are rendered inside the <label> element to make sure there is a strong connection between the error and its <input>, as required by the WCAG 2.0 standard.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Make sure your project is risk free

    Make sure your project is risk free

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Thomas Ploch, a Symfony contributor

    Thanks Thomas Ploch for being a Symfony contributor

    2 commits • 8 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?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • 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

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

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

      Deployed on

    Follow Symfony

    Search by Meilisearch