1

我有这个警告

group 已弃用:此 API 不是类型安全的,可能会导致 Closure Compiler 重命名问题。改用FormBuilder#group重载AbstractControlOptions。请注意, AbstractControlOptions期望validatorsasyncValidators成为验证者。如果您有自定义验证器,请确保它们的验证函数参数是AbstractControl而不是子类,例如FormGroup. 这些函数将使用类型对象调用,AbstractControl并且不能自动向下转换为子类,因此 TypeScript 将此视为错误。例如,将(group: FormGroup) => ValidationErrors|null 签名更改为(group: AbstractControl) => ValidationErrors|null.

如何修复此代码?

export function passwordMatchValidator(
  group: AbstractControl
): { invalidRePassword: boolean } | null {
  const passwordCtrl: AbstractControl | null = group.get('password');
  const passwordConfirmCtrl: AbstractControl | null = group.get(
    'passwordConfirm'
  );
  if (passwordCtrl && passwordConfirmCtrl) {
    if (passwordConfirmCtrl.touched || passwordConfirmCtrl.dirty) {
      if (passwordCtrl.value !== passwordConfirmCtrl.value) {
        passwordConfirmCtrl.setErrors({ invalidRePassword: true });
        return { invalidRePassword: true };
      }
    }
  }
  return null;
}

this.frm = this.fb.group({
    password: ['', Validators.required],
    passwordConfirm: ['', Validators.required],
    },
    { validator: passwordMatchValidator }
);
4

0 回答 0