angular - 自定义验证器在 Angular 10 的 FormGroup 中不起作用
问题描述
在我的反应形式中,我有两个密码字段。两个匹配两个字段的值我创建了一个自定义验证器,然后在 ts 文件的表单组中分配该验证器。代码如下:
this.registerForm = new FormGroup({
title: new FormControl("", Validators.required),
firstName: new FormControl("", Validators.required),
lastName: new FormControl("", Validators.required),
password: new FormControl("", [Validators.required, Validators.minLength(6)]),
confirmPassword: new FormControl("", Validators.required),
acceptTerms: new FormControl("", Validators.required)
},
{
validator: MustMatch('password', 'confirmPassword')
});
必须匹配验证器代码:
export function MustMatch(controlName: string, matchingControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName];
if (matchingControl.errors && !matchingControl.errors.mustMatch11111) {
// return if another validator has already found an error on the matchingControl
return;
}
// set error on matchingControl if validation fails
if (control.value !== matchingControl.value) {
matchingControl.setErrors({ mustMatch11111: true });
} else {
matchingControl.setErrors(null);
}
}
}
但 MustMatch 显示错误如下:
'{验证器类型的参数:(formGroup:FormGroup)=> void; }' 不可分配给类型为 'ValidatorFn | 的参数 验证器Fn[] | 抽象控制选项'。对象字面量只能指定已知属性,而 'validator' 类型中不存在 'ValidatorFn | 验证器Fn[] | 抽象控制选项'。
66 validator: MustMatch('password', 'confirmPassword')
由于我是角度新手,任何人都可以帮我解决这个问题。
解决方案
推荐阅读
- c# - 我的网络代码只是在我的本地网络中工作
- python - Python请求代理模块问题
- javascript - 反应Js | 无法读取未定义的属性“道具”
- javascript - Cucumber-JS 中的可选参数
- c# - 日期格式识别
- mp4 - 检测 h264 流中的 I 帧(分段 mp4)
- qlikview - 我想加载 2 个相同且来自相同源 QVD 的表,但其中一个我执行循环
- handlebars.js - 有没有办法在 schema.org 微数据中排除文本被读取?
- python - 如何将 xmlTree iterparse 应用于嵌套的 XML 集
- odata - 如何使用 Sitefinity oData(Web 服务)获取内容项的完整内容 URL 并将其添加到响应数据中?