Skip to content

Commit 0c50826

Browse files
committed
[Form] document $deep and $flatten of getErrors()
1 parent 4221db8 commit 0c50826

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

components/form/introduction.rst

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -663,36 +663,44 @@ 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, but only errors attached to this form level (e.g. "global errors)
689+
$errors = $form->getErrors(true);
682690

683-
.. note::
691+
// a FormErrorIterator instance, but only errors attached to this form level (e.g. "global errors)
692+
$errors = $form->getErrors(true, false);
684693

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.
694+
.. tip::
689695

690-
.. note::
696+
In older Symfony versions, ``getErrors()`` returned an array. To use the
697+
errors the same way in Symfony 2.5 or newer, you have to pass them to
698+
PHP's :phpfunction:`iterator_to_array` function::
699+
700+
$errorsAsArray = iterator_to_array($form->getErrors());
691701

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.
702+
This is useful, for example, if you want to use PHP's ``array_`` function
703+
on the form errors.
696704

697705
.. _Packagist: https://packagist.org/packages/symfony/form
698706
.. _Twig: http://twig.sensiolabs.org

0 commit comments

Comments
 (0)