I have Reactive Form using Angular Material inputs (mdInput
) that is initialized in the following manner using FormBuilder
:
contactForm: FormGroup;
this.contactForm = this.fb.group({
name: ['', [Validators.required, Validators.maxLength(128)]],
email: ['', [Validators.required, Validators.email]],
subject: ['', [Validators.required, Validators.maxLength(128)]],
message: ['', [Validators.required, Validators.maxLength(1000)]]
});
onSubmit(): void {
this.resetForm();
}
private resetForm(): void {
this.contactForm.reset();
}
With Angular Material inputs associated to the respective FormControl elements with hooking into (ngSubmit)
:
<form [formGroup]="contactForm" (ngSubmit)="onSubmit()">
<md-input-container>
<input mdInput formControlName="name" placeholder="Name">
</md-input-container>
<button type="submit" md-raised-button [disabled]="contactForm.invalid">Submit</button>
When calling reset()
on the FormGroup
contactForm
(this.contactForm.reset(), the form elements values are successfully cleared/replaced with empty strings, but the elements are both immediately dirty and touched with CSS classes
ng-invalid&&
ng-touchedpresent on the elements. Strangely they also have the
ng-pristineCSS class on them after the
reset()`.
What is the most effective way to approach resetting the form including clearing/resetting the FormControl
values and marking them as not touched and not dirty? Is it utilizing markAsUntouched()
or markAsPristine()
? Is it using setValue()
or reset()
with specific options? The goal is to reset the form as if the user is interacting with it for the first time.
Update: Here is a Stackblitz showing this issue in action.
Thank you for any help you can provide.
As mentioned by @Harry Ninh in the comments, using a regular button instead of
ngSubmit
will fix the behavior. This is because, by default, Material errors show when an input isinvalid
and eithertouched
orsubmitted
. There is a long thread about it here, but basically, callingreset()
on a form control or form group does not reset the actual form, just the values.You can do one of the following:
ViewChild
to get access to theFormGroupDirective
and reset it.See here: