Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb365b2e

Browse files
committed
[Form] document $deep and $flatten of getErrors()
1 parent652adfd commitb365b2e

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

‎components/form/introduction.rst‎

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,23 +620,67 @@ and the errors will display next to the fields on error.
620620
Accessing Form Errors
621621
~~~~~~~~~~~~~~~~~~~~~
622622

623+
..versionadded::2.5
624+
Before Symfony 2.5, ``getErrors()`` returned an array of ``FormError``
625+
objects. The return value was changed to ``FormErrorIterator`` in Symfony
626+
2.5.
627+
623628
You can use the:method:`Symfony\\Component\\Form\\FormInterface::getErrors`
624-
method to access the list of errors.Each element is a:class:`Symfony\\Component\\Form\\FormError`
625-
object::
629+
method to access the list of errors.It returns a
630+
:class:`Symfony\\Component\\Form\\FormErrorIterator` instance::
626631

627632
$form = ...;
628633

629634
// ...
630635

631-
//array of FormError objects
636+
//a FormErrorIterator instance
632637
$errors = $form->getErrors();
633638

634-
..note::
639+
..tip::
640+
641+
In older Symfony versions, ``getErrors()`` returned an array. To use the
642+
errors the same way in Symfony 2.5 or newer, you have to pass them to
643+
PHP's:phpfunction:`iterator_to_array` function::
644+
645+
$errorsAsArray = iterator_to_array($form->getErrors());
646+
647+
This is useful, for example, if you want to use PHP's ``array_`` function
648+
on the form errors.
649+
650+
Child Form Errors
651+
.................
652+
653+
..versionadded::2.5
654+
The ``$deep`` and ``$flatten`` arguments were introduced in Symfony 2.5.
655+
656+
By default, ``getErrors()`` only returns the errors of the form object it
657+
was called on. To also include the errors of child forms, you'll have to pass
658+
``true`` as the first argument to it::
659+
660+
// ...
661+
662+
// $errors contains all errors of the whole form tree
663+
$errors = $form->getErrors(true);
664+
665+
You can use the:method:`Symfony\\Component\\Form\\FormError::getOrigin` method
666+
on each element returned by the ``FormErrorIterator`` to determine the form
667+
the error belongs to.
668+
669+
You can also let the iterator reflect the form tree structure. To achieve
670+
this, pass ``false`` as the second argument to ``getErrors()``. The iterator
671+
then returns a ``FormErrorIterator`` object for each child form instead of
672+
multiple ``FormError`` childs. You can easily use a:phpclass:`RecursiveIteratorIterator`
673+
to iterate over all errors::
674+
675+
$form = ...;
676+
677+
// ...
635678

636-
Unless you enable the:ref:`error_bubbling<reference-form-option-error-bubbling>`
637-
option on a particular child form, ``getErrors()`` only returns the errors
638-
of the form it is accessed on. For debugging purposes, you can use the:method:`Symfony\\Component\\Form\\Form::getErrorsAsString` method which
639-
returns a string representation of all errors of the whole form tree.
679+
$errors = $form->getErrors(true, false);
680+
681+
foreach ($errors as $error) {
682+
// $error is a FormError instance
683+
}
640684

641685
.. _Packagist:https://packagist.org/packages/symfony/form
642686
.. _Twig:http://twig.sensiolabs.org

‎reference/forms/types/form.rst‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ The actual default value of this option depends on other field options:
7676
..include::/reference/forms/types/options/empty_data.rst.inc
7777
:start-after: DEFAULT_PLACEHOLDER
7878

79-
.. _reference-form-option-error-bubbling:
80-
8179
..include::/reference/forms/types/options/error_bubbling.rst.inc
8280

8381
..include::/reference/forms/types/options/error_mapping.rst.inc

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp