I am using angular5 reactivemodule to show a form in my application. I had also used required validator which will subsequently make the field in red color and show an error msg to the user.
It is working as expected but when I reset the form using
this.form.reset()
the form shows me a validation error that the specific field is required. I also used form.markAsPristine() or form.markAsUntouched() to make it work but the problem persist after applying multiple combination of possible pairs.
example.html
<form [formGroup]="checkForm" (ngSubmit)="submitForm()">
<mat-form-field>
<input matInput formControlName="name" placeholder="name" />
<mat-error *ngIf="checkForm.get('name').errors?.required">
Name is required.
</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput formControlName="email" placeholder="email" />
<mat-error *ngIf="checkForm.get('email').errors?.required">
Name is required.
</mat-error>
</mat-form-field>
<button [disabled]="checkForm.invalid" type="submit">add</button>
</form>
example.ts
checkForm = this.formBuilder.group({
'name': ['', Validators.required],
'email': ['', Validators.required]
});
submitForm() {
this.checkForm.reset();
// this.checkForm.markAsPristine();
this.checkForm.markAsUntouched();
}
Any help is appreciated.