diff --git a/components/form/introduction.rst b/components/form/introduction.rst index 825b2402c17..9222d5b42a0 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 :doc:`dynamic form modification `). + .. _Packagist: https://packagist.org/packages/symfony/form .. _Twig: http://twig.sensiolabs.org .. _`Twig Configuration`: http://twig.sensiolabs.org/doc/intro.html 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()) + ); + }