bie*_*las 5 validation typescript angular
我创建了一个简单的表单,我想向其中添加一个验证器:
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
});
}
Run Code Online (Sandbox Code Playgroud)
但在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;
}
Run Code Online (Sandbox Code Playgroud)
当我将表单的构造更改为FormBuilder它开始工作时 - 为什么会这样?
根据api 文档和示例,构造函数只是获取验证器函数,而不是像那样FormBuilder获取对象。
所以这应该有效:
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)])
},
PasswordValidators.passwordsShouldMatch
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10064 次 |
| 最近记录: |