【发布时间】:2017-08-07 09:06:36
【问题描述】:
我创建了一个简单的表单,我想在其中添加一个验证器:
form;
constructor() {
this.form = new FormGroup({
'old-password' : new FormControl('', [Validators.required, Validators.minLength(4)], PasswordValidators.checkPassword),
'new-password' : new FormControl('', [Validators.required, Validators.minLength(4)]),
'confirm-password' : new FormControl('', [Validators.required, Validators.minLength(4)])
}, {
validator: PasswordValidators.passwordsShouldMatch
});
}
但在validator 部分我收到一条错误消息:
[ts]
Argument of type '{ validator: (control: AbstractControl) => { passwordsShouldMatch: boolean; }; }' is not assignable to parameter of type 'ValidatorFn'.
Object literal may only specify known properties, and 'validator' does not exist in type 'ValidatorFn'.
(property) validator: (control: AbstractControl) => {
passwordsShouldMatch: boolean;
}
当我将表单的构造更改为 FormBuilder 时,它开始工作 - 这是为什么呢?
【问题讨论】:
-
您是否尝试通过提及您的自定义
validator来删除validator: -
可以发
PasswordValidators.passwordsShouldMatch方法吗?
标签: angular validation typescript