I have to reset my form along with validation. is there any method to reset the state of form from ng-dirty to ng-pristine.
-
1Possible duplicate of [Resetting a form in Angular 2 after submit](http://stackoverflow.com/questions/36655922/resetting-a-form-in-angular-2-after-submit) – Angular University Aug 18 '16 at 17:21
-
2This is the solution for now https://stackoverflow.com/a/48217303/9059790 – Dan Patil Feb 25 '20 at 05:34
-
This is the solution for above angular 10. https://stackoverflow.com/questions/49788215/angular-material-reseting-reactiveform-shows-validation-error/71108302#71108302 – chandoo Nov 15 '22 at 06:54
21 Answers
Here's how it currently works with Angular 4.1.0 - 5.1.3:
class YourComponent {
@ViewChild("yourForm")
yourForm: NgForm;
onSubmit(): void {
doYourThing();
// yourForm.reset(), yourForm.resetForm() don't work, but this does:
this.yourForm.form.markAsPristine();
this.yourForm.form.markAsUntouched();
this.yourForm.form.updateValueAndValidity();
}
}

- 11,111
- 10
- 71
- 96
-
2If you use Angular Material Inputs, you can make them valid again by using `yourForm.resetForm('')`. But you need the other code also. – ravo10 Oct 14 '18 at 22:12
-
15Unfortunately this doesn't seem to work for Angular 6.1.0, the input is marked as invalid. – Stijn Van Bael Oct 22 '18 at 14:23
-
In angular 6 define variable submit:boolean and give Initial value false. Then submit form submit value assign true. Then inside response submit value assign false and reset the form – Dilshan Dilip Aug 07 '20 at 15:51
-
this.supplerService.AddMetrics(add).subscribe( (myMetrics)=>{ console.log(myMetrics); this.resetForm(); this.viewMetrics(); // this.metricsform.controls.metricsName.setErrors(null); }, (err)=>{ console.log(err); } ); – Dilshan Dilip Aug 07 '20 at 15:52
-
resetForm(){ this.submitted = false; this.metricsform.reset(); } – Dilshan Dilip Aug 07 '20 at 16:03
-
1
from.resetForm()
I've tried pretty much everything, and the only thing I found that actually resets form.submitted to false is the following:
In your template, send your form into the submit function:
<form name="form" class="form-horizontal" (ngSubmit)="f.form.valid && submit(f)" #f="ngForm" novalidate>
In your component.ts file, have the following:
// import NgForm
import { NgForm } from '@angular/forms';
// get the passed in variable from the html file
submit(myForm: NgForm): void {
console.log(myForm.form.status, myForm.form.dirty, myForm.form.pristine, myForm.form.untouched, myForm.submitted, myForm.dirty, myForm.pristine, myForm.untouched);
// This is the key!
myForm.resetForm();
console.log(myForm.form.status, myForm.form.dirty, myForm.form.pristine, myForm.form.untouched, myForm.submitted, myForm.dirty, myForm.pristine, myForm.untouched);
}
The console.log values output the following - notice it resets all values.
VALID true false false true true false false
INVALID false true true false false true true

- 2,819
- 1
- 26
- 31
-
Works well but there is one problem. Status go to INVALID and stay so.It does not change to VALID with new values. A kind of bug maybe i don't know. I need to refresh page for new submit. – LacOniC Apr 25 '21 at 18:34
app.component.html
The #formDirective="ngForm"
and passing the formDirective
to the onSubmit
method is crucial for resetting error styles such as mat-error
. Check #4190 to follow this bug and in-depth explanation why it's needed and how it works under the hood.
<form [formGroup]="contactForm" (ngSubmit)="onSubmit(contactForm.value, formDirective)" #formDirective="ngForm">
<!-- Your input elements... -->
<button [disabled]="!contactForm.valid">Submit</button>
</form>
app.component.ts
Remember about the FormGroupDirective
which you need to import from @angular/forms
(Angular/Material 9). To make the form empty call this.contactForm.reset();
and the form will be invalid
, it's fine. However, you also need to reset the undesired validators styles, and this is a different method, i.e. formDirective.resetForm();
.
Notice difference between formDirective
and formData
with its different built-in methods.
import { FormGroupDirective } from '@angular/forms';
public contactForm: FormGroup = this.formBuilder.group({
// Your input elements...
});
public onSubmit(
formData: FormGroup,
formDirective: FormGroupDirective
): void {
this.contactForm.reset(); // Reset form data
formDirective.resetForm(); // Reset the ugly validators
}

- 8,508
- 6
- 68
- 94
-
2For future readers who struggled like me, this is the only working solution for Angular 9. – Gangadhar Jannu Jul 02 '20 at 22:36
-
1
-
<form #formDirective="ngForm" (ngSubmit)="submitForm(formDirective)">
And in your component class, call formDirective.resetForm():
private submitForm(formDirective): void {
formDirective.resetForm();
this.myForm.reset();
}

- 20,182
- 14
- 100
- 98
I appreciate everyones answers here. They pointed me in the right direction.
Angular 6 with Angular Material
<form #myForm="ngForm" [formGroup]="formGroup" (ngSubmit)="submit()">
then
@ViewChild('myForm') myForm: NgForm;
formGroup = new FormGroup({...});
submit() {
if (this.formGroup.pristine ||
this.formGroup.untouched ||
!this.formGroup.valid
) {
return;
}
.... do with form data
this.formGroup.reset();
this.myForm.resetForm();
}

- 2,414
- 3
- 27
- 48
There doesn't seem to be support for that yet. A workaround I have seen is to recreate the form after submit which is obviously cumbersome and ugly.
See also

- 623,577
- 216
- 2,003
- 1,567
-
4This is the solution for now https://stackoverflow.com/a/48217303/9059790 – Dan Patil Feb 25 '20 at 05:33
I'm doing this in RC.5, I define my form elements in a template.
<form (ngSubmit)="submit(); form.reset()" #form="ngForm">
Passing the form to submit or watching it with ViewChild
didn't work at all, this is good enough for me at the moment.

- 407
- 9
- 20
-
I used this. I am on angular2. Yes it reset the form. But it affects the validation. I can submit the form without anything in the fields. Eventhough the isValid property is false in that case, form does not show the alert messages. The reason should be the form getting reset. – vigamage May 18 '17 at 06:26
-
1It doesn't clear the pristines, errors hints and the Validator associated to the #form. – Maxime Flament Jun 05 '18 at 11:43
In more current versions (Angular 2.4.8 in my case) it's easy:
Marks a control as prestine and therefore valid again.
private resetFormControlValidation(control: AbstractControl) {
control.markAsPristine();
control.markAsUntouched();
control.updateValueAndValidity();
}

- 79
- 1
- 3
-
In my case the following has been enough (Angular 15): control.updateValueAndValidity(); – Bronek Apr 06 '23 at 12:13
This Solution in Angular 8 worked for me in Reactive Forms
Name your Form something like this
<form #regForm="ngForm">
Create Object of UI Form using ViewChild
@ViewChild('regForm', {static: false}) myForm: NgForm;
use this, after submit call.
this.myForm.resetForm();
submitted false your validations
this.submitted = false;

- 1,728
- 5
- 26
- 42

- 51
- 4
-
There's no need for this, simply calling reset on the formGroup object is enough- – chrismarx Apr 24 '20 at 22:07
Building up from Benny Bottema's answer, I was able to reset the form including validations using resetForm() in Angular 6.
class YourComponent {
@ViewChild("yourForm")
yourForm: NgForm;
onSubmit(): void {
doYourThing();
this.yourForm.resetForm();
}
}

- 835
- 15
- 17

- 148
- 1
- 8
This worked for me in Angular 5 using template driven forms (it will not work for reactive forms)
<form #myForm = "ngForm" (submit)="callMyAPI(); myForm.resetForm()">
This resets both form values and validations.

- 10,288
- 6
- 68
- 99

- 578
- 5
- 13
-
2@Ashwin , this will work for template driven forms (formsModule) and not for model driven forms (reactiveFormsModule). – Kumar Sidharth Jul 20 '18 at 07:42
On resetting by using YourformName.reset()
I was facing validation error issues.
So use YourformName.resetForm()
in your .ts file . It is working correctly now.
I am using template driven form in Angular btw.

- 7,277
- 11
- 36
- 55

- 31
- 1
I've recently considered this as there is currently (May 2016) nothing architected into Angular2 for this as yet.
Considering the user cannot enter 'undefined' or 'null' and the validation is mainly used to display form errors to the user then just reset the control values to Null
myControl.updateValue(null);
You will need to add this condition when deciding to display the control errors in your UI simply by adding
if (myControl.value !== undefined && myControl.value !== null) {
//then there is reason to display an error
}
(this check because Validators.Required treats blank content as valid)
Hope this helps.

- 5,736
- 1
- 35
- 36
If you are using Angular Material and using <mat-error>
, only way it worked for me is this.
You have to use FormGroupDirective.
@ViewChild(FormGroupDirective) formDirective: FormGroupDirective;
submitBtnClick() {
this.formDirective.resetForm();
}

- 315
- 3
- 10
if you use model-driven forms i.e ngFormModel, Defining the controlGroup once again after submitting will solve this.Refer this link

- 199
- 2
- 13
-
i have checked the link you have provided. The given method is working but i think that is not the right way to reset the form. Actually he is creating a new form on every submit instead of resetting. – Mubashir Jan 20 '16 at 05:13
-
1@Mubashir currently there is no other way. https://github.com/angular/angular/issues/4933, https://github.com/angular/angular/issues/6169 – Günter Zöchbauer Jan 20 '16 at 08:19
-
1
In the latest Angular versions you just need to run this.form.reset()
(it marks everything as pristine and untouched and all that stuff under the hood) and then update value and validity for all the child form controls/groups/arrays.
Unfortunately, there's no direct way to do this for the nested controls without manual children traversal, at least for now.

- 247
- 3
- 14
Angular Reactive Forms
onCancel(): void {
this.registrationForm.reset();
this.registrationForm.controls['name'].setErrors(null);
this.registrationForm.controls['email'].setErrors(null);
}

- 29
- 2
Just loop over the form controls and make them pristine
(Object as any).values(this.mainForm.form.controls).forEach((control: FormControl) => {
control.markAsUntouched();
control.markAsPristine();
});

- 424
- 2
- 7
After looking at all the answers ,
this is the only thing i got to work ...
(Object as any).values(this.formGroup.controls).forEach((control: FormControl) => {
control.setErrors(null);
});

- 152
- 5
- 15
Make sure that your button type = button !!!
by default html button get type="submit"
Angular 8 Form reset validation errors
Create form:
this.userForm = this.formBuilder.group({
firstName: ['', [Validators.required, Validators.minLength(2)]],
email: ['', [Validators.required, Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')]]});
Reset form:
this.userForm.reset();
this.userForm.controls.userEmail.setErrors(null);

- 63
- 1
- 1