-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix the definition of customizing form's global errors. #3543
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 3 commits
236d06b
1e278c4
e3b20d8
6f6fcca
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 |
---|---|---|
|
@@ -771,8 +771,17 @@ and customize the ``form_errors`` fragment. | |
See :ref:`cookbook-form-theming-methods` for how to apply this customization. | ||
|
||
You can also customize the error output for just one specific field type. | ||
For example, certain errors that are more global to your form (i.e. not specific | ||
to just one field) are rendered separately, usually at the top of your form: | ||
To customize *only* the markup used for these errors, follow the same directions | ||
as above but put the contents in a relative ``_errors`` block (or file in case | ||
of PHP templates). For example: ``text_errors`` (or ``text_errors.html.php``). | ||
|
||
.. tip:: | ||
|
||
See :ref:`form-template-blocks` to find out which specific block or file you | ||
have to customize. | ||
|
||
Certain errors that are more global to your form (i.e. not specific to just one | ||
field) are rendered separately, usually at the top of your form: | ||
|
||
.. configuration-block:: | ||
|
||
|
@@ -785,9 +794,50 @@ to just one field) are rendered separately, usually at the top of your form: | |
<?php echo $view['form']->render($form); ?> | ||
|
||
To customize *only* the markup used for these errors, follow the same directions | ||
as above, but now call the block ``form_errors`` (Twig) / the file ``form_errors.html.php`` | ||
(PHP). Now, when errors for the ``form`` type are rendered, your customized | ||
fragment will be used instead of the default ``form_errors``. | ||
as above, but now check if the ``compound`` variable is set to ``true``. | ||
|
||
.. tip:: | ||
|
||
If the ``compound`` variable is ``true``, it means that what's being | ||
currently rendered is a collection of fields (e.g. a whole form), and not | ||
just an individual field. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{# form_errors.html.twig #} | ||
{% block form_errors %} | ||
{% spaceless %} | ||
{% if errors|length > 0 %} | ||
{% if compound %} | ||
<ul> | ||
{% for error in errors %} | ||
<li>{{ error.message }}</li> | ||
{% endfor %} | ||
</ul> | ||
{% else %} | ||
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 is now an |
||
{# display the errors for a single field #} | ||
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.
|
||
{% endif %} | ||
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. What about the else? We're not trying to customize the else case here, but of course without it, non-compound errors won't show up - so we'll need something :) 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. btw, @mtrojanowski happy to see you around here lately :) |
||
{% endif %} | ||
{% endspaceless %} | ||
{% endblock form_errors %} | ||
|
||
.. code-block:: html+php | ||
|
||
<!-- form_errors.html.php --> | ||
<?php if ($errors): ?> | ||
<?php if ($compound): ?> | ||
<ul> | ||
<?php foreach ($errors as $error): ?> | ||
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. I think our standard is to put a space before 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. Really? 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. I always did it, and afaik I have started creating php template code blocks in the docs. 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 should indeed be consistent. Can you link to an example? At least, the templating chapter doesn't use spaces this way. 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. No spaces - check out the |
||
<li><?php echo $error->getMessage() ?></li> | ||
<?php endforeach; ?> | ||
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 never use the trailing semi colon when using "templating PHP" 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. Then I guess we should - shows up in |
||
</ul> | ||
<?php else: ?> | ||
<?php // render the errors for a single field ?> | ||
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.
|
||
<?php endif; ?> | ||
<?php endif ?> | ||
|
||
|
||
Customizing the "Form Row" | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
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.
I added the part about the
compound
var as a tip. Or would you rather leave it as a regular sentence in the text?@weaverryan @wouterj
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.
I would put it in a regular sentence