Angular Material: Reseting reactiveform shows validation error

前端 未结 3 1541
南方客
南方客 2020-12-10 05:16

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 05:35

    All you need to do is avoid (ngSubmit) method on your reactive form:
    1. Remove (ngSubmit)="submitForm()" from your form tag
    1. Change your form button type from 'submit' to 'button'
    2. On this button you want to set a click action to (click)="submitForm()"
    3. in the submitForm() method do:

    this.checkForm.markAsPristine();
    this.checkForm.markAsUntouched();
    

    This will submit the form and reset its value without alerting validators

提交回复
热议问题