Skip to content

[Form] Ability to clear form errors #9908

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 2 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
8 changes: 8 additions & 0 deletions components/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,14 @@ method to access the list of errors. It returns a
// a FormErrorIterator instance representing the form tree structure
$errors = $form->getErrors(true, false);

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 </form/dynamic_form_modification>`).


Learn more
----------

Expand Down
23 changes: 23 additions & 0 deletions form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,26 @@ field according to the current selection in the ``sport`` field:
The major benefit of submitting the whole form to just extract the updated
``position`` field is that no additional server-side code is needed; all the
code from above to generate the submitted form can be reused.

Clearing Form Errors
~~~~~~~~~~~~~~~~~~~~

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())
);
}