From 6abe3e46d584cb0873d392993b09feb5aa140c91 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Mon, 6 Apr 2015 10:32:37 -0400 Subject: [PATCH 1/3] Documentation on the new clearErrors() method --- components/form/introduction.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/form/introduction.rst b/components/form/introduction.rst index 825b2402c17..34869784f27 100644 --- a/components/form/introduction.rst +++ b/components/form/introduction.rst @@ -697,6 +697,13 @@ method to access the list of errors. It returns a This is useful, for example, if you want to use PHP's ``array_`` function on the form errors. +Clearing Form Errors +~~~~~~~~~~~~~~~~~~~~ + +Any errors can be manually cleared using the :method:`Symfony\\Component\\Form\\FormInterface::clearErrors` +method. This is useful when you'd like to validate the form without showing validation errors to the user +(i.e. during a partial AJAX submission or :ref:`dynamic form modification `). + .. _Packagist: https://packagist.org/packages/symfony/form .. _Twig: http://twig.sensiolabs.org .. _`Twig Configuration`: http://twig.sensiolabs.org/doc/intro.html From 274dd451e163f84ed8c1d6dd4e22cca9dfe7c692 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Mon, 6 Apr 2015 10:39:59 -0400 Subject: [PATCH 2/3] Add clearErrors() example to cookbook entry on dynamic form modification --- cookbook/form/dynamic_form_modification.rst | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index 9107ae6a89a..2e72fa144c0 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -731,3 +731,26 @@ all of this, use a listener:: By doing this, you may accidentally disable something more than just form validation, since the ``POST_SUBMIT`` event may have other listeners. + +Clearing Form Errors +~~~~~~~~~~~~~~~~~~~~ + +Alternatively, if you want to perform validation but not show errors to the user +during the AJAX reload, you could instead clear them before rendering the form:: + + public function createAction(Request $request) + { + $meetup = new SportMeetup(); + $form = $this->createForm(new SportMeetupType(), $meetup); + $form->handleRequest($request); + if ($form->isValid()) { + // ... save the meetup, redirect etc. + } + + $form->clearErrors(true); + + return $this->render( + 'AppBundle:Meetup:create.html.twig', + array('form' => $form->createView()) + ); + } From a5863fd81cfe6cfdc4d190cf7ba43ef60ff0b902 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Tue, 7 Apr 2015 08:47:25 -0400 Subject: [PATCH 3/3] Fix link to cookbook article --- components/form/introduction.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/form/introduction.rst b/components/form/introduction.rst index 34869784f27..9222d5b42a0 100644 --- a/components/form/introduction.rst +++ b/components/form/introduction.rst @@ -702,7 +702,7 @@ Clearing Form Errors Any errors can be manually cleared using the :method:`Symfony\\Component\\Form\\FormInterface::clearErrors` method. This is useful when you'd like to validate the form without showing validation errors to the user -(i.e. during a partial AJAX submission or :ref:`dynamic form modification `). +(i.e. during a partial AJAX submission or :doc:`dynamic form modification `). .. _Packagist: https://packagist.org/packages/symfony/form .. _Twig: http://twig.sensiolabs.org