How to reset form validation on submission of the form in ANGULAR 2

Mubashir picture Mubashir · Jan 5, 2016 · Viewed 69k times · Source

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.

Answer

Benny Bottema picture Benny Bottema · May 26, 2017

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