Skip to content

Commit e510364

Browse files
committed
[Form] document $deep and $flatten of getErrors()
1 parent 652adfd commit e510364

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

components/form/introduction.rst

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,23 +620,72 @@ 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+
// ...
635661

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.
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 an ``FormErrorIterator`` object for each child form instead
672+
of multiple ``FormError`` childs::
673+
674+
$form = ...;
675+
676+
// ...
677+
678+
$errors = $form->getErrors(true, false);
679+
680+
foreach ($errors as $error) {
681+
if ($error->hasChildren()) {
682+
// handle errors of a child form
683+
// $error is an instance of FormErrorIterator
684+
} else {
685+
// handle an error of $form
686+
// $error is an instance of FormError
687+
}
688+
}
640689

641690
.. _Packagist: https://packagist.org/packages/symfony/form
642691
.. _Twig: http://twig.sensiolabs.org

0 commit comments

Comments
 (0)