我遇到了与这里相似的问题:
我已经阅读过上述问题,但问题仍然出现在代码检查工具中:
group已弃用:此API不是类型安全的,可能会导致Closure Compiler重命名的问题。请改用带有AbstractControlOptions的
FormBuilder#group
重载。注意,AbstractControlOptions
期望validators
和asyncValidators
为有效的验证器。如果您有自定义验证器,请确保它们的验证函数参数是AbstractControl
,而不是子类,例如FormGroup
。这些函数将使用AbstractControl
类型的对象调用,无法自动向下转换为子类,因此TypeScript会将其视为错误。例如,将(group: FormGroup) => ValidationErrors|null
签名更改为(group: AbstractControl) => ValidationErrors|null
。(已弃用)
下面是我的formBuilder:
registerForm = this._fb.group({
firstname: ["", [Validators.required]],
lastname: ["", [Validators.required]],
email: ["", [Validators.required, Validators.email]],
password: ["", [PasswordValidator.strength]],
confirmPassword: ["", [Validators.required]]
}, {
validator: PasswordValidator.confirmed("password", "confirmPassword")
});
我的验证器:
export class PasswordValidator {
static confirmed = (controlName: string, matchingControlName: string) => {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName];
if (matchingControl.errors && !matchingControl.errors.confirmedValidator) {
return null;
}
if (control.value !== matchingControl.value) {
matchingControl.setErrors({ confirmedValidator: true });
return ({ confirmedValidator: true });
} else {
matchingControl.setErrors(null);
return null;
}
};
}
}
有什么想法吗?我正在返回正确的对象 :/。
validator: PasswordValidator.confirmed("password", "confirmPassword")
引起的。 - Owen Kelvins
...validators: [PasswordValidator.confirmed("password", "confirmPassword")]
- Owen Kelvin