Skip to content

Commit 766e01f

Browse files
committed
feature #4169 [Components][Form] document $deep and $flatten of getErrors() (xabbuh)
This PR was merged into the 2.5 branch. Discussion ---------- [Components][Form] document $deep and $flatten of getErrors() | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#9918) | Applies to | 2.5+ | Fixed tickets | #3660 This is based on #4168. Commits ------- 0245e91 [Form] document $deep and $flatten of getErrors() 4221db8 describe how to access form errors
2 parents 08ca28a + 0245e91 commit 766e01f

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

components/form/introduction.rst

+27-11
Original file line numberDiff line numberDiff line change
@@ -663,29 +663,45 @@ 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+
671+
.. versionadded:: 2.5
672+
The ``$deep`` and ``$flatten`` arguments were introduced in Symfony 2.5.
673+
666674
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::
675+
method to access the list of errors. It returns a
676+
:class:`Symfony\\Component\\Form\\FormErrorIterator` instance::
669677

670678
$form = ...;
671679

672680
// ...
673681

674-
// an array of FormError objects, but only errors attached to this form level (e.g. "global errors)
682+
// a FormErrorIterator instance, but only errors attached to this form level (e.g. "global errors)
675683
$errors = $form->getErrors();
676684

677-
// an array of FormError objects, but only errors attached to the "firstName" field
685+
// a FormErrorIterator instance, but only errors attached to the "firstName" field
678686
$errors = $form['firstName']->getErrors();
679687

680-
// a string representation of all errors of the whole form tree
681-
$errors = $form->getErrorsAsString();
688+
// a FormErrorIterator instance in a flattened structure
689+
// use getOrigin() to determine the form causing the error
690+
$errors = $form->getErrors(true);
682691

683-
.. note::
692+
// a FormErrorIterator instance representing the form tree structure
693+
$errors = $form->getErrors(true, false);
694+
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());
684702

685-
If you enable the :ref:`error_bubbling <reference-form-option-error-bubbling>`
686-
option on a field, calling ``getErrors()`` on the parent form will include
687-
errors from that field. However, there is no way to determine which field
688-
an error was originally attached to.
703+
This is useful, for example, if you want to use PHP's ``array_`` function
704+
on the form errors.
689705

690706
.. _Packagist: https://packagist.org/packages/symfony/form
691707
.. _Twig: http://twig.sensiolabs.org

0 commit comments

Comments
 (0)