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.
import { FormsModule } from '@angular/forms'; In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.
In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.
To reset template-driven form, NgForm provides resetForm() method that is called as following. To call the above function, create a button in UI. If we want to reset form with some default values, then assign the default values with form control name of the form.
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();
}
}
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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With