Bootstrap 4 Form Theme

Bootstrap 4 Form Theme

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. {# templates/base.html.twig #}
  2. {# beware that the blocks in your template may be named different #}
  3. {% block head_css %}
  4. <!-- Copy CSS from https://getbootstrap.com/docs/4.4/getting-started/introduction/#css -->
  5. {% endblock %}
  6. {% block head_js %}
  7. <!-- Copy JavaScript from https://getbootstrap.com/docs/4.4/getting-started/introduction/#js -->
  8. {% 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:

  • YAML

    1. # config/packages/twig.yaml
    2. twig:
    3. form_themes: ['bootstrap_4_layout.html.twig']
  • XML

    1. <!-- config/packages/twig.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:twig="http://symfony.com/schema/dic/twig"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/twig
    9. https://symfony.com/schema/dic/twig/twig-1.0.xsd">
    10. <twig:config>
    11. <twig:form-theme>bootstrap_4_layout.html.twig</twig:form-theme>
    12. <!-- ... -->
    13. </twig:config>
    14. </container>
  • PHP

    1. // config/packages/twig.php
    2. $container->loadFromExtension('twig', [
    3. 'form_themes' => [
    4. 'bootstrap_4_layout.html.twig',
    5. ],
    6. // ...
    7. ]);

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. {# this tag only applies to the forms defined in this template #}
  3. {% form_theme form 'bootstrap_4_layout.html.twig' %}
  4. {% block body %}
  5. <h1>User Sign Up:</h1>
  6. {{ form(form) }}
  7. {% endblock %}

Error Messages

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. To achieve this, form_errors() is called by form_label() internally. If you call to form_errors() in your template, you’ll get the error messages displayed twice.

Tip

Since form errors are rendered inside the <label>, you cannot use CSS :after to append an asterisk to the label, because it would be displayed after the error message. Use the label option instead.

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().

File inputs

File inputs are rendered using the Bootstrap “custom-file” class, which hides the name of the selected file. To fix that, use the bs-custom-file-input JavaScript plugin, as recommended by Bootstrap Forms documentation.

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

New in version 4.4: Support for the switch-custom class was introduced in Symfony 4.4.

Bootstrap 4 has a feature called “custom forms”. You can enable that on your Symfony Form RadioType and CheckboxType by adding some classes to the label:

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

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