Skip to content

Commit 727fc74

Browse files
committed
minor #9351 Adding a documentation page about Bootstrap 4 form theme (Nyholm)
This PR was squashed before being merged into the master branch (closes #9351). Discussion ---------- Adding a documentation page about Bootstrap 4 form theme The Boostrap 4 theme has some "special" feature that we should mention. * I wanted to give the user a copy-paste get started template. * I mention WCAG2.0 because we should be proud of that and make users aware of it * The special classes in "custom forms" are not boostrap classes. They are feature flags in the Symfony boostrap 4 theme. * The half "labels and erros" is copied from the `form_custimazation.rst`. Commits ------- 696a366 Adding a documentation page about Bootstrap 4 form theme
2 parents e0fc9a0 + 696a366 commit 727fc74

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

form/bootstrap4.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
Bootstrap 4 Form Theme
2+
======================
3+
4+
Symfony provides several ways of integrating Bootstrap into your application. The
5+
most straightforward way is to just 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 head_css %}
15+
<!-- Copy CSS from https://getbootstrap.com/docs/4.0/getting-started/introduction/#css -->
16+
{% endblock %}
17+
{% block head_js %}
18+
<!-- Copy JavaScript from https://getbootstrap.com/docs/4.0/getting-started/introduction/#js -->
19+
{% endblock head %}
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 4 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_4_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+
http://symfony.com/schema/dic/services/services-1.0.xsd
46+
http://symfony.com/schema/dic/twig
47+
http://symfony.com/schema/dic/twig/twig-1.0.xsd">
48+
49+
<twig:config>
50+
<twig:form-theme>bootstrap_4_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', array(
59+
'form_themes' => array(
60+
'bootstrap_4_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:: twig
70+
71+
{# ... #}
72+
{# this tag only applies to the forms defined in this template #}
73+
{% form_theme form 'bootstrap_4_layout.html.twig' %}
74+
75+
{% block body %}
76+
<h1>User Sign Up:</h1>
77+
{{ form(form) }}
78+
{% endblock body %}
79+
80+
Accessibility
81+
-------------
82+
83+
The Bootstrap 4 framework has done a good job making it accessible for functional
84+
variations like impaired vision and cognitive ability. Symfony has taken this one
85+
step further to make sure the form theme complies with the `WCAG 2.0 standard`_.
86+
87+
This does not mean that your entire website automatically complies with the full
88+
standard, but it does mean that you have come far in your work to create a design
89+
for **all** users.
90+
91+
Custom Forms
92+
------------
93+
94+
Bootstrap 4 has a feature called "`custom forms`_". You can enable that on your
95+
Symfony Form ``RadioType`` and ``CheckboxType`` by adding a class called ``radio-custom``
96+
and ``checkbox-custom`` respectively.
97+
98+
.. code-block:: html+twig
99+
100+
{{ form_row(form.myRadio, {attr: {class: 'radio-custom'} }) }}
101+
{{ form_row(form.myCheckbox, {attr: {class: 'checkbox-custom'} }) }}
102+
103+
Labels and Errors
104+
-----------------
105+
106+
When you use the Bootstrap form themes and render the fields manually, calling
107+
``form_label()`` for a checkbox/radio field doesn't render anything. Due to Bootstrap
108+
internals, the label is already rendered by ``form_widget()``.
109+
110+
Form errors are rendered **inside** the ``<label>`` element to make sure there
111+
is a strong connection between the error and its ``<input>``, as required by the
112+
`WCAG 2.0 standard`_.
113+
114+
.. _`their documentation`: https://getbootstrap.com/docs/4.0/
115+
.. _`WCAG 2.0 standard`: https://www.w3.org/TR/WCAG20/
116+
.. _`custom forms`: https://getbootstrap.com/docs/4.0/components/forms/#custom-forms

form/form_customization.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ fragment needed to render every part of a form:
127127
calling ``form_label()`` for a checkbox/radio field doesn't show anything.
128128
Due to Bootstrap internals, the label is already shown by ``form_widget()``.
129129

130+
.. tip::
131+
132+
Read more about the :doc:`Bootstrap 4 form theme </form/bootstrap4>`.
133+
130134
In the next section you will learn how to customize a theme by overriding
131135
some or all of its fragments.
132136

0 commit comments

Comments
 (0)