提交表格后如何清除验证错误

时间:2020-02-12 08:53:37

标签: angular angular-material

提交表单后,我无法清除验证错误的错误。我在提交后通过form.resetForm()重置了表单,但仍然无法清除mat错误的验证。我也尝试过重新初始化表单。请您帮忙吗?谢谢inAdvance。

这里是code

3 个答案:

答案 0 :(得分:1)

使用ngForm从这里获取参考

In your Html file

<form  fxFlex fxLayout="column" fxLayoutGap="10px" [formGroup]="noteForm" #noteForm="ngForm">

In your ts file

 @ViewChild('noteForm', { static: true }) noteForm: NgForm;
//to reset form
this.noteForm.resetForm();

相应地在HTML和TS文件中替换名称。

答案 1 :(得分:0)

您不需要ngForm。您可以简单地将控件的错误设置为null

onSubmitchngPwd() {
    this.changepwdform.get("currentpassword").setErrors(null);  // set errors to null
}

示例:https://stackblitz.com/edit/angular-en6coj

答案 2 :(得分:0)

在材质设计中,垫错误会在控件被触摸或弄脏时触发,因此我们需要在重置表格后将其标记为未触摸控件,以下是使控件保持不变的代码:

this.pwdform.controls.newpassword.markAsUntouched(); this.pwdform.controls.currentpassword.markAsUntouched(); this.pwdform.controls.confirmpassword.markAsUntouched();