简体   繁体   English

从反应形式进行验证无法正常工作

[英]Validation from reactive forms doesnt work properly

I've got a problem with my reactive forms - my repeatPassword doesn't refresh. 我的反应式表单有问题-我的repeatPassword没有刷新。 If I write something in "password" label, then in "repeatPassword" and again in "password" the second one is not marked as invalid. 如果我在“密码”标签中写了一些内容,则在“ repeatPassword”中再次在“密码”中写第二个未标记为无效。

So I decided to do something like that: 所以我决定做这样的事情:

if (this.form.get('password').value !== this.form.get('passwordRepeat').value) {
  this.form.get('passwordRepeat').setErrors({'invalid': true});
}

if (this.form.get('password').value === this.form.get('passwordRepeat').value &&
  !this.form.get('passwordRepeat').hasError) {
  this.form.get('passwordRepeat').setErrors({'invalid': null});
}

The code looks fine until I try to set invalid to null - reactive forms treat it like an error, so I can't submit and ngif shows an error. 在我尝试将invalid设置为null之前,代码看起来还不错-反应性表单将其视为错误,因此我无法提交并且ngif显示错误。

The same error occurs when I'm trying to accept rules - unchecking checkbox doesn't mark it as invalid. 当我尝试接受规则时会发生相同的错误-取消选中复选框不会将其标记为无效。

Is there any option that forces validators to run once again? 是否有任何选项可以迫使验证程序再次运行?

Have you tried setting this.form.get('passwordRepeat').setErrors(null); 您是否尝试过设置this.form.get('passwordRepeat').setErrors(null); instead of this.form.get('passwordRepeat').setErrors({'invalid': null}); 而不是this.form.get('passwordRepeat').setErrors({'invalid': null}); ?

Also this.form.get('passwordRepeat').setErrors({'invalid': false}); 还有this.form.get('passwordRepeat').setErrors({'invalid': false}); should work properly. 应该正常工作。

See this answer on SO that explains how to set validation errors in component. 请参阅SO上的此答案,该答案说明了如何在组件中设置验证错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM