动态生成的复选框的自定义验证器不起作用

发布于 2025-01-24 23:55:01 字数 1095 浏览 3 评论 0原文

我已经在Angular 13版本中创建了一个项目,以验证动态生成的复选框。我已经关注了这个博客( https://coryrylan.com/博客/create-a-dynamic-checkbox-list-in-Angular )。在此博客中,有一个自定义验证。但是验证对我不起作用。自定义验证器代码是:

function minSelectedCheckboxes(min = 1) {
    const myValidator: ValidatorFn = (formArray: FormArray) => {
        const totalSelected = formArray.controls
            .map(control => control.value)
            .reduce((prev, next) => next ? prev + next : prev, 0);
        return totalSelected >= min ? null : { required: true };
    };

    return myValidator;
}

但是我遇到以下错误。谁能帮助我解决错误?

src/app/app.component.ts:62:8-错误ts2322:type'(formarray:formarray: formarray)=> {必需:布尔值; } | null'不能分配输入 “验证器”。参数类型“ formarray”和“控制”是 不相容。 类型“ AbstractControl”缺少“ formarray”类型的以下属性:控制,at,pusp,插入和6个。

62 const myValidator:validatorFn =(formArray:formArray)=> { ~~~~~~~~~~~

​net/d1qe4.png“ alt =”在此处输入图像描述>

I have created a project in Angular 13 version for validating the dynamically generated checkboxes. I have followed this blog (https://coryrylan.com/blog/creating-a-dynamic-checkbox-list-in-angular). In this blog, there is a custom validation. But the validation is not working for me. The custom validator code is:

function minSelectedCheckboxes(min = 1) {
    const myValidator: ValidatorFn = (formArray: FormArray) => {
        const totalSelected = formArray.controls
            .map(control => control.value)
            .reduce((prev, next) => next ? prev + next : prev, 0);
        return totalSelected >= min ? null : { required: true };
    };

    return myValidator;
}

But I am getting the below error. Can anyone help me to solve the error?

src/app/app.component.ts:62:8 - error TS2322: Type '(formArray:
FormArray) => { required: boolean; } | null' is not assignable to type
'ValidatorFn'. Types of parameters 'formArray' and 'control' are
incompatible.
Type 'AbstractControl' is missing the following properties from type 'FormArray': controls, at, push, insert, and 6 more.

62 const myValidator: ValidatorFn = (formArray: FormArray) => {
~~~~~~~~~~~

enter image description here

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

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

发布评论

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

评论(1

柠檬 2025-01-31 23:55:01

验证器具有以下类型:

export declare interface ValidatorFn {
    (control: AbstractControl): ValidationErrors | null;
}

因此您需要更改这样的功能:

function minSelectedCheckboxes(min = 1) {
  const myValidator: ValidatorFn = (control: AbstractControl) => {
    const formArray = control as FormArray;
    const totalSelected = formArray.controls
      .map((control) => control.value)
      .reduce((prev, next) => (next ? prev + next : prev), 0);
    return totalSelected >= min ? null : { required: true };
  };

  return myValidator;
}

ValidatorFn has the following type:

export declare interface ValidatorFn {
    (control: AbstractControl): ValidationErrors | null;
}

So you need to change the function like this:

function minSelectedCheckboxes(min = 1) {
  const myValidator: ValidatorFn = (control: AbstractControl) => {
    const formArray = control as FormArray;
    const totalSelected = formArray.controls
      .map((control) => control.value)
      .reduce((prev, next) => (next ? prev + next : prev), 0);
    return totalSelected >= min ? null : { required: true };
  };

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