首页 > 解决方案 > Angular FormBuilder 组已弃用

问题描述

我看到有一个主题看起来像我的,但它并没有真正回答我的问题,因为我们没有以相同的方式管理错误。FormBuilder 组已弃用

首先,我刚刚迁移到 Angular 11,现在我遇到了这个问题:

group is deprecated: This api is not typesafe and can result in issues with Closure Compiler renaming.
Use the `FormBuilder#group` overload with `AbstractControlOptions` instead.

在这个页面中,我会在我的页面上自动生成许多表单,在日期范围的情况下,我使用两个日期选择器。我创建了一个函数来检查 2 个日期选择器中的值。

零件:

newGroup = this.fb.group(
        {
          [node.type + '_' + node.objectId + '_dateFrom']: [
            '',
            [Validators.required]
          ],
          [node.type + '_' + node.objectId + '_dateTo']: [
            '',
            [Validators.required]
          ]
        },
        {
          validator: CheckFromToDate(
            node.type + '_' + node.objectId + '_dateFrom',
            node.type + '_' + node.objectId + '_dateTo'
          )
        }
      );

验证器:

export function CheckFromToDate(fromName: string, toName: string) {
  return (formGroup: FormGroup) => {
    const from = formGroup.controls[fromName];
    const to = formGroup.controls[toName];
    const dateFrom = new Date(from.value);
    const dateTo = new Date(to.value);
    const today = new Date();
    if (to.errors && from.errors) {
      // return if another validator has already found an error on the matchingControl
      return;
    }
    if (!from.value) {
      from.setErrors({ wrongDate: true });
      to.setErrors(null);
    } else if (!to.value) {
      to.setErrors({ wrongDate: true });
      from.setErrors(null);
    } else if (dateFrom.getTime() < -3600000) {
      from.setErrors({ wrongDate: true });
      to.setErrors(null);
    } else if (dateFrom > today) {
      from.setErrors({ wrongDate: true });
      to.setErrors(null);
    } else if (dateTo.getTime() < -3600000) {
      to.setErrors({ wrongDate: true });
      from.setErrors(null);
    } else if (dateTo > today) {
      to.setErrors({ wrongDate: true });
      from.setErrors(null);
    } else if (dateFrom.getTime() > dateTo.getTime()) {
      from.setErrors({ fromTo: true });
      to.setErrors({ fromTo: true });
    } else {
      from.setErrors(null);
      to.setErrors(null);
    }
  };
}

如何让我的验证器使用 Angular 11 中处理验证器的新方法?

标签: angularangular2-formsangular-validationangular2-formbuilderangular2-form-validation

解决方案


更改您的验证人签名

return (formGroup: FormGroup) 

return (controls: AbstractControl) 

然后更改您访问控件的方式

const from = formGroup.controls[fromName];

const from= controls.get(fromName);

还要更改您报告错误的方式

from.setErrors({ wrongDate: true });

return from.setErrors({ wrongDate: true }); // note the return statement.

最后一个改变是改变

newGroup = this.fb.group(
        {
           ...
        },
        {
          validator: CheckFromToDate(
            node.type + '_' + node.objectId + '_dateFrom',
            node.type + '_' + node.objectId + '_dateTo'
          )
        }
      );

newGroup = this.fb.group(
        {
           ...
        },
        {
          validator: CheckFromToDate(
            node.type + '_' + node.objectId + '_dateFrom',
            node.type + '_' + node.objectId + '_dateTo'
          )
        } as AbstractControlOptions
      );

请注意转换为AbstractControlOptions并且应该删除已弃用的警告。

详细解释可以参考官方文档


推荐阅读