@@ -663,36 +663,44 @@ and the errors will display next to the fields on error.
663
663
Accessing Form Errors
664
664
~~~~~~~~~~~~~~~~~~~~~
665
665
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
+
666
674
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 ::
669
677
670
678
$form = ...;
671
679
672
680
// ...
673
681
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)
675
683
$errors = $form->getErrors();
676
684
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
678
686
$errors = $form['firstName']->getErrors();
679
687
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 );
682
690
683
- .. note ::
691
+ // a FormErrorIterator instance, but only errors attached to this form level (e.g. "global errors)
692
+ $errors = $form->getErrors(true, false);
684
693
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 ::
689
695
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());
691
701
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.
696
704
697
705
.. _Packagist : https://packagist.org/packages/symfony/form
698
706
.. _Twig : http://twig.sensiolabs.org
0 commit comments