Skip to content

Commit 4ce7a15

Browse files
committed
bug #3543 Fix the definition of customizing form's global errors. (mtrojanowski)
This PR was merged into the 2.3 branch. Discussion ---------- Fix the definition of customizing form's global errors. The documentation for customizing global errors was actually still referring to the way it was done in 2.0. (where two different blocks were used: `field_errors` and `form_errors`). Since 2.1. only `form_errors` is used and errors are differentiated using the `compound` variable (set to `true` for form errors). | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3+ | Fixed tickets | #3542 Commits ------- 6f6fcca Applied some changes according to comments. e3b20d8 Minor rewording. Added tip about meaning of `compound` var and expanded the example code to contain `else` statements. 1e278c4 Added note about customizing errors for different field types. 236d06b Fix the definition of customizing form's global errors.
2 parents 2192c32 + 6f6fcca commit 4ce7a15

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

cookbook/form/form_customization.rst

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,17 @@ and customize the ``form_errors`` fragment.
771771
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
772772

773773
You can also customize the error output for just one specific field type.
774-
For example, certain errors that are more global to your form (i.e. not specific
775-
to just one field) are rendered separately, usually at the top of your form:
774+
To customize *only* the markup used for these errors, follow the same directions
775+
as above but put the contents in a relative ``_errors`` block (or file in case
776+
of PHP templates). For example: ``text_errors`` (or ``text_errors.html.php``).
777+
778+
.. tip::
779+
780+
See :ref:`form-template-blocks` to find out which specific block or file you
781+
have to customize.
782+
783+
Certain errors that are more global to your form (i.e. not specific to just one
784+
field) are rendered separately, usually at the top of your form:
776785

777786
.. configuration-block::
778787

@@ -785,9 +794,46 @@ to just one field) are rendered separately, usually at the top of your form:
785794
<?php echo $view['form']->render($form); ?>
786795
787796
To customize *only* the markup used for these errors, follow the same directions
788-
as above, but now call the block ``form_errors`` (Twig) / the file ``form_errors.html.php``
789-
(PHP). Now, when errors for the ``form`` type are rendered, your customized
790-
fragment will be used instead of the default ``form_errors``.
797+
as above, but now check if the ``compound`` variable is set to ``true``. If it
798+
is ``true``, it means that what's being currently rendered is a collection of
799+
fields (e.g. a whole form), and not just an individual field.
800+
801+
.. configuration-block::
802+
803+
.. code-block:: html+jinja
804+
805+
{# form_errors.html.twig #}
806+
{% block form_errors %}
807+
{% spaceless %}
808+
{% if errors|length > 0 %}
809+
{% if compound %}
810+
<ul>
811+
{% for error in errors %}
812+
<li>{{ error.message }}</li>
813+
{% endfor %}
814+
</ul>
815+
{% else %}
816+
{# ... display the errors for a single field #}
817+
{% endif %}
818+
{% endif %}
819+
{% endspaceless %}
820+
{% endblock form_errors %}
821+
822+
.. code-block:: html+php
823+
824+
<!-- form_errors.html.php -->
825+
<?php if ($errors): ?>
826+
<?php if ($compound): ?>
827+
<ul>
828+
<?php foreach ($errors as $error): ?>
829+
<li><?php echo $error->getMessage() ?></li>
830+
<?php endforeach; ?>
831+
</ul>
832+
<?php else: ?>
833+
<!-- ... render the errors for a single field -->
834+
<?php endif; ?>
835+
<?php endif ?>
836+
791837

792838
Customizing the "Form Row"
793839
~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)