来自反应式表单的验证无法正常工作
Validation from reactive forms doesnt work properly
我的反应式表单出现问题 - 我的 repeatPassword 没有刷新。如果我在 "password" 标签中写了一些东西,那么在 "repeatPassword" 和 "password" 中,第二个不会被标记为无效。
所以我决定这样做:
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});
}
在我尝试将 invalid 设置为 null 之前,代码看起来不错 - 反应形式将其视为错误,因此我无法提交并且 ngif 显示错误。
当我尝试接受规则时出现同样的错误 - 取消选中复选框不会将其标记为无效。
是否有任何选项可以强制验证者再次 运行?
你试过设置吗
this.form.get('passwordRepeat').setErrors(null);
代替
this.form.get('passwordRepeat').setErrors({'invalid': null});
?
另外 this.form.get('passwordRepeat').setErrors({'invalid': false});
应该可以正常工作。
解释了如何在组件中设置验证错误。
我的反应式表单出现问题 - 我的 repeatPassword 没有刷新。如果我在 "password" 标签中写了一些东西,那么在 "repeatPassword" 和 "password" 中,第二个不会被标记为无效。
所以我决定这样做:
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});
}
在我尝试将 invalid 设置为 null 之前,代码看起来不错 - 反应形式将其视为错误,因此我无法提交并且 ngif 显示错误。
当我尝试接受规则时出现同样的错误 - 取消选中复选框不会将其标记为无效。
是否有任何选项可以强制验证者再次 运行?
你试过设置吗
this.form.get('passwordRepeat').setErrors(null);
代替
this.form.get('passwordRepeat').setErrors({'invalid': null});
?
另外 this.form.get('passwordRepeat').setErrors({'invalid': false});
应该可以正常工作。