简体   繁体   English

Angular 中自定义验证器的问题

[英]Problems with the custom validator in Angular

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:在构建表单的第 8 行中,出现以下错误:

TS2345: Argument of type '{ validator: (c: AbstractControl) => ValidationErrors; TS2345:'{ 验证器类型的参数:(c: AbstractControl) => ValidationErrors; }' is not assignable to parameter of type 'ValidatorFn | }' 不可分配给 'ValidatorFn | 类型的参数ValidatorFn[] |验证器[] | AbstractControlOptions'.抽象控制选项'。 Object literal may only specify known properties, and 'validator' does not exist in type 'ValidatorFn |对象字面量只能指定已知属性,并且类型“ValidatorFn | 中不存在”“validator” ValidatorFn[] |验证器[] | AbstractControlOptions'.抽象控制选项'。

Any ideas?有任何想法吗?

You should use validators not validator :您应该使用validators而不是validator

this.form = new FormGroup({
 // fields
}, { validators: this.matchingPasswords });

Perhaps it's also better to move that matchingPasswords to an exported utility function, but that's just personal code preference.也许将matchingPasswords的密码移动到导出的实用程序 function 也更好,但这只是个人代码偏好。

Other working notations are:其他工作符号是:

this.form = new FormGroup({
 // fields
}, this.matchingPasswords);
this.form = new FormGroup({
 // fields
}, [this.matchingPasswords]);

Use validators instead of validator使用验证器而不是验证器

{ validators: this.matchingPasswords }

and this和这个

public matchingPasswords: ValidatorFn = (c: AbstractControl): ValidationErrors | null => {
        const password = c.get('passwords');
        const confirmPassword = c.get('passwordTemp');

        if (password && confirmPassword && password.value !== confirmPassword.value) {
            return { mismatchedPasswords: true };
        }

        return null;
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM