Skip to content

Commit 652adfd

Browse files
committed
describe how to access form errors
1 parent 936cb0f commit 652adfd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

components/form/introduction.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,27 @@ and the errors will display next to the fields on error.
617617
For a list of all of the built-in validation constraints, see
618618
:doc:`/reference/constraints`.
619619

620+
Accessing Form Errors
621+
~~~~~~~~~~~~~~~~~~~~~
622+
623+
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::
626+
627+
$form = ...;
628+
629+
// ...
630+
631+
// array of FormError objects
632+
$errors = $form->getErrors();
633+
634+
.. note::
635+
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.
640+
620641
.. _Packagist: https://packagist.org/packages/symfony/form
621642
.. _Twig: http://twig.sensiolabs.org
622643
.. _`Twig Configuration`: http://twig.sensiolabs.org/doc/intro.html

reference/forms/types/form.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ The actual default value of this option depends on other field options:
7676
.. include:: /reference/forms/types/options/empty_data.rst.inc
7777
:start-after: DEFAULT_PLACEHOLDER
7878

79+
.. _reference-form-option-error-bubbling:
80+
7981
.. include:: /reference/forms/types/options/error_bubbling.rst.inc
8082

8183
.. include:: /reference/forms/types/options/error_mapping.rst.inc

0 commit comments

Comments
 (0)