-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Adding a documentation page about Bootstrap 4 form theme #9351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
6effb03
8a3b9cb
5657f66
9852b4c
3757a02
dc1fced
747712e
0b47211
69f5d68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
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): | ||
|
||
.. code-block:: html+twig | ||
|
||
{# 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 head %} | ||
|
||
If your application uses modern front-end practices, it's better to use | ||
:doc:`Webpack Encore </frontend>` and follow :doc:`this tutorial </frontend/encore/bootstrap>` | ||
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: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# config/packages/twig.yaml | ||
twig: | ||
form_themes: ['bootstrap_4_layout.html.twig'] | ||
|
||
.. code-block:: xml | ||
|
||
<!-- 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> | ||
|
||
.. code-block:: php | ||
|
||
// 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: | ||
|
||
.. code-block:: twig | ||
|
||
{# ... #} | ||
{# 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 body %} | ||
|
||
Accessibility | ||
------------- | ||
|
||
The Bootstrap 4 framework has done a good job making it accessible for function | ||
variations like impaired vision and cognitive ability. Symfony has taken this one | ||
step further to make sure the form theme complies with the `WCAG2.0 standard`_. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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. | ||
|
||
.. code-block:: html+twig | ||
|
||
{{ form_row(form.myRadio, {attr: {class: 'radio-custom'} }) }} | ||
{{ form_row(form.myCheckbox, {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 show anything. Due to Bootstrap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you |
||
internals, the label is already shown by ``form_widget()``. | ||
|
||
You may also note that the form errors are rendered **inside** the ``<label>``. This | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could remove this ->
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
is done to make sure there is a strong connection between the error and in | ||
``<input>``, as required by the `WCAG2.0 standard`_. | ||
|
||
.. _`their documentation`: https://getbootstrap.com/docs/4.0/ | ||
.. _`WCAG2.0 standard`: https://www.w3.org/TR/WCAG20/ | ||
.. _`custom forms`: https://getbootstrap.com/docs/4.0/components/forms/#custom-forms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just asking: does this term sounds good to you? ->
function variations
Should it befunctional variations
or evenfunctional diversity
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure. It is the perfect word in Swedish. We may need a native English speaker.
My intent is to not have the word "disabilities" or anything that could be connected to a negative tone.