Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2.1K
Views
Angular Reactive forms : how to reset form state and keep values after submit

In an Angular reactive form. How to reset only the state of form after successful submit?

Here is the process:

  • Create the form and setValue from service result
  • Modify values and submit the form
  • If form is properly submitted to service, then reset and keep values

How to keep values as modified and reset the form to its pristine state.

A form.reset() simply empty the form. But if I don't call it, the state is not reset and for example my validations depending on form state classes (pristine, dirty, valid etc.) are still there.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The solution of @smnbbrv works pretty well.

You can also provide your actual form value to reset() method.

Given the fact myReactiveForm is a Reactive form in your component. After successful submit of your form (by calling a service for example), then your can do:

this.myReactiveForm.reset(this.myReactiveForm.value);

It will reset the form and set the "new" form values to the same value you form had.

This method can be see within Tour of Hero example Official Angular.io doc

about 4 years ago · Santiago Trujillo Report

0

That's pretty much easy:

this.form.markAsPristine();
this.form.markAsUntouched();

This resets the form metadata and the form is pristine again

about 4 years ago · Santiago Trujillo Report

0

Caution for users of @angular/components (material)

There's an added complication if you're using angular material controls, with mat-error to display your errors. Such errors are displayed based on the result of an ErrorStateMatcher (in addition to any *ngIf you may have applied).

The default ErrorStateMatcher displays errors based on a boolean:

isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean 
{
   return !!(control && control.invalid && (control.touched || (form && form.submitted)));
}

So what this means in English is:

If the form has ever been submitted and one if its controls becomes invalid (per validation rules) then the error will be displayed.

This is often not what you want (especially if you're reading this question!).

Looking at the source code - there isn't a way to set submitted = false unless you call resetForm on the ngForm directive (not the FormGroup object). Setting states as pristine or untouched don't reset submitted to false.

If you're using mat-error you may find it easier to create your own ErrorStateMatcher (simple interface) for custom logic if this is an issue.

Note: ErrorStateMatcher is only in angular material - and not a part of standard angular forms.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!