Skip to content

Commit 75d6afa

Browse files
committed
Add documenation for bootstrap 5 form template
1 parent c970ec2 commit 75d6afa

File tree

1 file changed

+101
-10
lines changed

1 file changed

+101
-10
lines changed

form/bootstrap5.rst

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,67 @@ If you prefer to apply the Bootstrap styles on a form to form basis, include the
7979

8080
.. _reference-forms-bootstrap-error-messages:
8181

82-
General
83-
-------
82+
.. note::
8483

85-
By default, all inputs are rendered with the ``mb-3`` class on their container.
84+
By default, all inputs are rendered with the ``mb-3`` class on their container.
85+
If you override the `row_attr` class option, the ``mb-3`` will be override too
86+
and you will need to explicitly add it.
8687

8788
Error Messages
8889
--------------
8990

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*.
91+
Unlike the Bootstrap 4 template, errors are rendered **after** the ``input`` element.
92+
However, this still make a strong connection between the error and its ``<input>``, as
93+
required by the `WCAG 2.0 standard`_.
9594

9695
Checkboxes and Radios
9796
---------------------
9897

9998
For a checkbox/radio field, calling ``form_label()`` doesn't render anything.
10099
Due to Bootstrap internals, the label is already rendered by ``form_widget()``.
101100

101+
Inline Checkboxes and Radios
102+
----------------------------
103+
104+
If you want to render your checkboxes or radios fields `inline`_, you can add
105+
the `checkbox-inline` or `radio-inline` class, depending of your Symfony Form
106+
type or `ChoiceType` configuration, to the label class.
107+
108+
.. configuration-block::
109+
110+
.. code-block:: php
111+
112+
$builder
113+
->add('myCheckbox', CheckboxType::class, [
114+
'label_attr' => [
115+
'class' => '`checkbox-inline',
116+
],
117+
])
118+
->add('myRadio', RadioType::class, [
119+
'label_attr' => [
120+
'class' => 'radio-inline',
121+
],
122+
]);
123+
124+
.. code-block:: twig
125+
126+
{{ form_row(form.myCheckbox, {
127+
label_attr: {
128+
class: 'checkbox-inline'
129+
}
130+
}) }}
131+
132+
{{ form_row(form.myRadio, {
133+
label_attr: {
134+
class: 'radio-inline'
135+
}
136+
}) }}
137+
102138
Switches
103139
________
104140

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:
141+
Bootstrap 5 allow to render checkboxes as "`switches`_". You can enable this feature
142+
on your Symfony Form ``CheckboxType`` by adding the ``checkbox-switch`` class to the label:
107143

108144
.. configuration-block::
109145

@@ -123,6 +159,54 @@ Symfony Form ``CheckboxType`` by adding the ``checkbox-switch`` class to the lab
123159
}
124160
}) }}
125161
162+
.. tip::
163+
164+
You can also render your switches inline by simply adding the ``checkbox-inline`` class
165+
on the ``label_attr`` option.
166+
167+
.. code-block:: php
168+
169+
...
170+
'label_attr' => [
171+
'class' => '`checkbox-inline checkbox-switch',
172+
],
173+
...
174+
175+
.. caution::
176+
177+
Switches only worked with **checkbox**.
178+
179+
Input group
180+
___________
181+
182+
To create `input group`_ in your Symfony Form, simply add the `input-group` class
183+
to the ``row_attr`` option.
184+
185+
.. configuration-block::
186+
187+
.. code-block:: php
188+
189+
$builder->add('email', CheckboxType::class, [
190+
'label' => '@',
191+
'row_attr' => [
192+
'class' => 'input-group',
193+
],
194+
]);
195+
196+
.. code-block:: twig
197+
198+
{{ form_row(form.email, {
199+
label: '@',
200+
row_attr: {
201+
class: 'input-group'
202+
}
203+
}) }}
204+
205+
.. caution::
206+
207+
If you have filled in the `help` option of your form, it will also be rendered
208+
as part of the group.
209+
126210
Floating labels
127211
---------------
128212

@@ -156,6 +240,11 @@ of your form type.
156240
}
157241
}) }}
158242
243+
.. caution::
244+
245+
You **must** provide a ``label`` and a ``placeholder`` to make floating labels
246+
works properly.
247+
159248
Accessibility
160249
-------------
161250

@@ -168,5 +257,7 @@ standard, but it does mean that you have come far in your work to create a desig
168257
for **all** users.
169258

170259
.. _`WCAG 2.0 standard`: https://www.w3.org/TR/WCAG20/
260+
.. _`inline`: https://getbootstrap.com/docs/5.0/forms/checks-radios/#inline
171261
.. _`switches`: https://getbootstrap.com/docs/5.0/forms/checks-radios/#switches
262+
.. _`input group`: https://getbootstrap.com/docs/5.0/forms/input-group/
172263
.. _`floating label`: https://getbootstrap.com/docs/5.0/forms/floating-labels/

0 commit comments

Comments
 (0)