@@ -663,36 +663,45 @@ and the errors will display next to the fields on error.
663663Accessing 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+
666674You 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);
684694
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.
695+ .. tip ::
689696
690- .. note ::
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());
691702
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.
703+ This is useful, for example, if you want to use PHP's ``array_ `` function
704+ on the form errors.
696705
697706.. _Packagist : https://packagist.org/packages/symfony/form
698707.. _Twig : http://twig.sensiolabs.org
0 commit comments