Skip to content

[Form] Ability to clear form errors #5152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions components/form/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </cookbook/form/dynamic_form_modification>`).

.. _Packagist: https://packagist.org/packages/symfony/form
.. _Twig: http://twig.sensiolabs.org
.. _`Twig Configuration`: http://twig.sensiolabs.org/doc/intro.html
23 changes: 23 additions & 0 deletions cookbook/form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
}