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

Commite2441a1

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

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

‎components/form/introduction.rst‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,14 @@ and the errors will display next to the fields on error.
663663
Accessing Form Errors
664664
~~~~~~~~~~~~~~~~~~~~~
665665

666+
..versionadded::2.5
667+
Before Symfony 2.5, ``getErrors()`` returned an array of ``FormError``
668+
objects. The return value was changed to ``FormErrorIterator`` in Symfony
669+
2.5.
670+
666671
You can use the:method:`Symfony\\Component\\Form\\FormInterface::getErrors`
667-
method to access the list of errors.Each element is a:class:`Symfony\\Component\\Form\\FormError`
668-
object::
672+
method to access the list of errors.It returns a
673+
:class:`Symfony\\Component\\Form\\FormErrorIterator` instance::
669674

670675
$form = ...;
671676

@@ -687,12 +692,51 @@ object::
687692
errors from that field. However, there is no way to determine which field
688693
an error was originally attached to.
689694

690-
..note::
695+
..tip::
696+
697+
In older Symfony versions, ``getErrors()`` returned an array. To use the
698+
errors the same way in Symfony 2.5 or newer, you have to pass them to
699+
PHP's:phpfunction:`iterator_to_array` function::
700+
701+
$errorsAsArray = iterator_to_array($form->getErrors());
702+
703+
This is useful, for example, if you want to use PHP's ``array_`` function
704+
on the form errors.
705+
706+
Child Form Errors
707+
.................
708+
709+
..versionadded::2.5
710+
The ``$deep`` and ``$flatten`` arguments were introduced in Symfony 2.5.
711+
712+
By default, ``getErrors()`` only returns the errors of the form object it
713+
was called on. To also include the errors of child forms, you'll have to pass
714+
``true`` as the first argument to it::
715+
716+
// ...
717+
718+
// $errors contains all errors of the whole form tree
719+
$errors = $form->getErrors(true);
720+
721+
You can use the:method:`Symfony\\Component\\Form\\FormError::getOrigin` method
722+
on each element returned by the ``FormErrorIterator`` to determine the form
723+
the error belongs to.
724+
725+
You can also let the iterator reflect the form tree structure. To achieve
726+
this, pass ``false`` as the second argument to ``getErrors()``. The iterator
727+
then returns a ``FormErrorIterator`` object for each child form instead of
728+
multiple ``FormError`` childs. You can easily use a:phpclass:`RecursiveIteratorIterator`
729+
to iterate over all errors::
730+
731+
$form = ...;
732+
733+
// ...
691734

692-
Unless you enable the:ref:`error_bubbling<reference-form-option-error-bubbling>`
693-
option on a particular child form, ``getErrors()`` only returns the errors
694-
of the form it is accessed on. For debugging purposes, you can use the:method:`Symfony\\Component\\Form\\Form::getErrorsAsString` method which
695-
returns a string representation of all errors of the whole form tree.
735+
$errors = $form->getErrors(true, false);
736+
737+
foreach ($errors as $error) {
738+
// $error is a FormError instance
739+
}
696740

697741
.. _Packagist:https://packagist.org/packages/symfony/form
698742
.. _Twig:http://twig.sensiolabs.org

‎reference/forms/types/form.rst‎

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

81-
.. _reference-form-option-error-bubbling:
82-
8381
..include::/reference/forms/types/options/error_bubbling.rst.inc
8482

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp