|
| 1 | +Bootstrap 5 Form Theme |
| 2 | +====================== |
| 3 | + |
| 4 | +Symfony provides several ways of integrating Bootstrap into your application. The |
| 5 | +most straightforward way is to add the required ``<link>`` and ``<script>`` |
| 6 | +elements in your templates (usually you only include them in the main layout |
| 7 | +template which other templates extend from): |
| 8 | + |
| 9 | +.. code-block:: html+twig |
| 10 | + |
| 11 | + {# templates/base.html.twig #} |
| 12 | + |
| 13 | + {# beware that the blocks in your template may be named different #} |
| 14 | + {% block stylesheets %} |
| 15 | + <!-- Copy CSS from https://getbootstrap.com/docs/5.0/getting-started/introduction/#css --> |
| 16 | + {% endblock %} |
| 17 | + {% block javascripts %} |
| 18 | + <!-- Copy JavaScript from https://getbootstrap.com/docs/5.0/getting-started/introduction/#js --> |
| 19 | + {% endblock %} |
| 20 | + |
| 21 | +If your application uses modern front-end practices, it's better to use |
| 22 | +:doc:`Webpack Encore </frontend>` and follow :doc:`this tutorial </frontend/encore/bootstrap>` |
| 23 | +to import Bootstrap's sources into your SCSS and JavaScript files. |
| 24 | + |
| 25 | +The next step is to configure the Symfony application to use Bootstrap 5 styles |
| 26 | +when rendering forms. If you want to apply them to all forms, define this |
| 27 | +configuration: |
| 28 | + |
| 29 | +.. configuration-block:: |
| 30 | + |
| 31 | + .. code-block:: yaml |
| 32 | +
|
| 33 | + # config/packages/twig.yaml |
| 34 | + twig: |
| 35 | + form_themes: ['bootstrap_5_layout.html.twig'] |
| 36 | +
|
| 37 | + .. code-block:: xml |
| 38 | +
|
| 39 | + <!-- config/packages/twig.xml --> |
| 40 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 41 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 42 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 43 | + xmlns:twig="http://symfony.com/schema/dic/twig" |
| 44 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 45 | + https://symfony.com/schema/dic/services/services-1.0.xsd |
| 46 | + http://symfony.com/schema/dic/twig |
| 47 | + https://symfony.com/schema/dic/twig/twig-1.0.xsd"> |
| 48 | +
|
| 49 | + <twig:config> |
| 50 | + <twig:form-theme>bootstrap_5_layout.html.twig</twig:form-theme> |
| 51 | + <!-- ... --> |
| 52 | + </twig:config> |
| 53 | + </container> |
| 54 | +
|
| 55 | + .. code-block:: php |
| 56 | +
|
| 57 | + // config/packages/twig.php |
| 58 | + $container->loadFromExtension('twig', [ |
| 59 | + 'form_themes' => [ |
| 60 | + 'bootstrap_5_layout.html.twig', |
| 61 | + ], |
| 62 | +
|
| 63 | + // ... |
| 64 | + ]); |
| 65 | +
|
| 66 | +If you prefer to apply the Bootstrap styles on a form to form basis, include the |
| 67 | +``form_theme`` tag in the templates where those forms are used: |
| 68 | + |
| 69 | +.. code-block:: html+twig |
| 70 | + |
| 71 | + {# ... #} |
| 72 | + {# this tag only applies to the forms defined in this template #} |
| 73 | + {% form_theme form 'bootstrap_5_layout.html.twig' %} |
| 74 | + |
| 75 | + {% block body %} |
| 76 | + <h1>User Sign Up:</h1> |
| 77 | + {{ form(form) }} |
| 78 | + {% endblock %} |
| 79 | + |
| 80 | +.. _reference-forms-bootstrap-error-messages: |
| 81 | + |
| 82 | +General |
| 83 | +------- |
| 84 | + |
| 85 | +By default, all inputs are rendered with the ``mb-3`` class on their container. |
| 86 | + |
| 87 | +Error Messages |
| 88 | +-------------- |
| 89 | + |
| 90 | +Form errors are rendered **inside** the ``<label>`` element to make sure there |
| 91 | +is a strong connection between the error and its ``<input>``, as required by the |
| 92 | +`WCAG 2.0 standard`_. To achieve this, ``form_errors()`` is called by |
| 93 | +``form_label()`` internally. If you call to ``form_errors()`` in your template, |
| 94 | +you'll get the error messages displayed *twice*. |
| 95 | + |
| 96 | +Checkboxes and Radios |
| 97 | +--------------------- |
| 98 | + |
| 99 | +For a checkbox/radio field, calling ``form_label()`` doesn't render anything. |
| 100 | +Due to Bootstrap internals, the label is already rendered by ``form_widget()``. |
| 101 | + |
| 102 | +Switches |
| 103 | +________ |
| 104 | + |
| 105 | +Bootstrap 5 allow to render checkboxes as "`switches`_". You can enable that on your |
| 106 | +Symfony Form ``CheckboxType`` by adding the ``checkbox-switch`` class to the label: |
| 107 | + |
| 108 | +.. configuration-block:: |
| 109 | + |
| 110 | + .. code-block:: php |
| 111 | +
|
| 112 | + $builder->add('myCheckbox', CheckboxType::class, [ |
| 113 | + 'label_attr' => [ |
| 114 | + 'class' => 'checkbox-switch', |
| 115 | + ], |
| 116 | + ]); |
| 117 | +
|
| 118 | + .. code-block:: twig |
| 119 | +
|
| 120 | + {{ form_row(form.myCheckbox, { |
| 121 | + label_attr: { |
| 122 | + class: 'checkbox-switch' |
| 123 | + } |
| 124 | + }) }} |
| 125 | +
|
| 126 | +Floating labels |
| 127 | +--------------- |
| 128 | + |
| 129 | +To render an input field with a `floating label`_, you must add a ``label``, |
| 130 | +a ``placeholder`` and the ``form-floating`` class to the ``row_attr`` option |
| 131 | +of your form type. |
| 132 | + |
| 133 | +.. configuration-block:: |
| 134 | + |
| 135 | + .. code-block:: php |
| 136 | +
|
| 137 | + $builder->add('name', TextType::class, [ |
| 138 | + 'label' => 'Name', |
| 139 | + 'attr' => [ |
| 140 | + 'placeholder' => 'Name', |
| 141 | + ], |
| 142 | + 'row_attr' => [ |
| 143 | + 'class' => 'form-floating', |
| 144 | + ], |
| 145 | + ]); |
| 146 | +
|
| 147 | + .. code-block:: twig |
| 148 | +
|
| 149 | + {{ form_row(form.name, { |
| 150 | + label: 'Name', |
| 151 | + attr: { |
| 152 | + placeholder: 'Name' |
| 153 | + }, |
| 154 | + row_attr: { |
| 155 | + class: 'form-floating' |
| 156 | + } |
| 157 | + }) }} |
| 158 | +
|
| 159 | +Accessibility |
| 160 | +------------- |
| 161 | + |
| 162 | +The Bootstrap 5 framework has done a good job making it accessible for functional |
| 163 | +variations like impaired vision and cognitive ability. Symfony has taken this one |
| 164 | +step further to make sure the form theme complies with the `WCAG 2.0 standard`_. |
| 165 | + |
| 166 | +This does not mean that your entire website automatically complies with the full |
| 167 | +standard, but it does mean that you have come far in your work to create a design |
| 168 | +for **all** users. |
| 169 | + |
| 170 | +.. _`WCAG 2.0 standard`: https://www.w3.org/TR/WCAG20/ |
| 171 | +.. _`switches`: https://getbootstrap.com/docs/5.0/forms/checks-radios/#switches |
| 172 | +.. _`floating label`: https://getbootstrap.com/docs/5.0/forms/floating-labels/ |
0 commit comments