Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Components][Form] document $deep and $flatten of getErrors() #4169

Merged
merged 2 commits into from
Nov 20, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions components/form/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -663,29 +663,45 @@ and the errors will display next to the fields on error.
Accessing Form Errors
~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 2.5
Before Symfony 2.5, ``getErrors()`` returned an array of ``FormError``
objects. The return value was changed to ``FormErrorIterator`` in Symfony
2.5.

.. versionadded:: 2.5
The ``$deep`` and ``$flatten`` arguments were introduced in Symfony 2.5.

You can use the :method:`Symfony\\Component\\Form\\FormInterface::getErrors`
method to access the list of errors. Each element is a :class:`Symfony\\Component\\Form\\FormError`
object::
method to access the list of errors. It returns a
:class:`Symfony\\Component\\Form\\FormErrorIterator` instance::

$form = ...;

// ...

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

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

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

.. note::
// a FormErrorIterator instance representing the form tree structure
$errors = $form->getErrors(true, false);

.. tip::

In older Symfony versions, ``getErrors()`` returned an array. To use the
errors the same way in Symfony 2.5 or newer, you have to pass them to
PHP's :phpfunction:`iterator_to_array` function::

$errorsAsArray = iterator_to_array($form->getErrors());

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

.. _Packagist: https://packagist.org/packages/symfony/form
.. _Twig: http://twig.sensiolabs.org
Expand Down