Angular 中自定义验证器的问题
我知道这个问题已经在许多其他帖子中提出过,但在所有提出的解决方案中,我找不到一个可以为我解决问题的解决方案。
我正在尝试创建一个验证器来检查输入的确认密码是否与密码相同。
这是我的构建表单代码:
this.form = new FormGroup({
name: new FormControl('', [Validators.required]),
surname : new FormControl('', [Validators.required]),
username: new FormControl('', [Validators.required, Validators.email]),
tempUsername: new FormControl('', [Validators.required, Validators.email]),
password: new FormControl('', [Validators.required, Validators.minLength(8), Validators.maxLength(16)]),
passwordTemp: new FormControl('', [Validators.required]),
}, { validator: this.matchingPasswords }
);
}
这是我的验证器方法:
public matchingPasswords(c: AbstractControl): ValidationErrors | null {
const password = c.get(['passwords']);
const confirmPassword = c.get(['passwordTemp']);
if (password.value !== confirmPassword.value) {
return { mismatchedPasswords: true };
}
return null;
}
在构建表单的第 8 行中,我收到以下错误:
TS2345:类型参数 '{ validator: (c: AbstractControl) =>验证错误; }' 不可分配给类型为 'ValidatorFn | 的参数验证器Fn[] |抽象控制选项'。 对象文字只能指定已知属性,并且类型“ValidatorFn |”中不存在“validator”验证器Fn[] |抽象控制选项'。
有什么想法吗?
I know that the question in question has already been asked in many other posts but in all the proposed solutions I cannot find one that solves the problem for me.
I'm trying to create a validator to check that the entered confirmation password is the same as the password.
This is my build form code:
this.form = new FormGroup({
name: new FormControl('', [Validators.required]),
surname : new FormControl('', [Validators.required]),
username: new FormControl('', [Validators.required, Validators.email]),
tempUsername: new FormControl('', [Validators.required, Validators.email]),
password: new FormControl('', [Validators.required, Validators.minLength(8), Validators.maxLength(16)]),
passwordTemp: new FormControl('', [Validators.required]),
}, { validator: this.matchingPasswords }
);
}
and this is my validator method:
public matchingPasswords(c: AbstractControl): ValidationErrors | null {
const password = c.get(['passwords']);
const confirmPassword = c.get(['passwordTemp']);
if (password.value !== confirmPassword.value) {
return { mismatchedPasswords: true };
}
return null;
}
in line 8 of the build form I get the following error:
TS2345: Argument of type '{ validator: (c: AbstractControl) => ValidationErrors; }' is not assignable to parameter of type 'ValidatorFn | ValidatorFn[] | AbstractControlOptions'. Object literal may only specify known properties, and 'validator' does not exist in type 'ValidatorFn | ValidatorFn[] | AbstractControlOptions'.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
validators
而不是validator
:也许将
matchingPasswords
移至导出的实用程序函数也更好,但这只是个人代码偏好。其他工作符号有:
You should use
validators
notvalidator
:Perhaps it's also better to move that
matchingPasswords
to an exported utility function, but that's just personal code preference.Other working notations are:
使用验证器而不是验证器
,这
Use validators instead of validator
and this