Angular 中自定义验证器的问题

发布于 2025-01-11 00:04:59 字数 1280 浏览 4 评论 0原文

我知道这个问题已经在许多其他帖子中提出过,但在所有提出的解决方案中,我找不到一个可以为我解决问题的解决方案。

我正在尝试创建一个验证器来检查输入的确认密码是否与密码相同。

这是我的构建表单代码:

    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 }
    );
  }

这是我的验证器方法:

  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;
  }

在构建表单的第 8 行中,我收到以下错误:

TS2345:类型参数 '{ validator: (c: AbstractControl) =>验证错误; }' 不可分配给类型为 'ValidatorFn | 的参数验证器Fn[] |抽象控制选项'。   对象文字只能指定已知属性,并且类型“ValidatorFn |”中不存在“validator”验证器Fn[] |抽象控制选项'。

有什么想法吗?

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:

TS2345: Argument of type '{ validator: (c: AbstractControl) => ValidationErrors; }' is not assignable to parameter of type 'ValidatorFn | ValidatorFn[] | AbstractControlOptions'.   Object literal may only specify known properties, and 'validator' does not exist in type 'ValidatorFn | ValidatorFn[] | AbstractControlOptions'.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2025-01-18 00:04:59

您应该使用 validators 而不是 validator

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

也许将 matchingPasswords 移至导出的实用程序函数也更好,但这只是个人代码偏好。

其他工作符号有:

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

You should use validators not 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.

Other working notations are:

this.form = new FormGroup({
 // fields
}, this.matchingPasswords);
this.form = new FormGroup({
 // fields
}, [this.matchingPasswords]);
彼岸花似海 2025-01-18 00:04:59

使用验证器而不是验证器

{ validators: this.matchingPasswords }

,这

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;
    };

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;
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文