【问题标题】:angular2 validation: finding current validators on a controlangular2 验证:在控件上查找当前验证器
【发布时间】:2016-11-30 17:05:20
【问题描述】:

Angular RC4 如何根据其他 FormControls 或 FormGroups 的状态添加和删除验证。

基于这篇帖子Angular2 validator which relies on multiple form fields

在示例中 - 它使用 .compose 来设置多个验证器。它用新的 Validator.compose 替换现有的 Validator

this.form.controls["empID"].validator = Validators.compose([Validators.pattern("[0-9]{7}"), Validators.required]);
this.form.controls["empID"].updateValueAndValidity();

我的问题是我找不到 FormControl(.add 或 .remove)来更改当前验证(一次一个验证器)。如果我想添加/删除 Validators.required,我将不得不拉出当前的验证器(另一个问题),然后在新的 .compose 中添加/删除 require。 (上例)

我是不是在想这个过程。要动态添加验证器,但前提是我知道所有验证器。

最后一件事我试图通过传入一个控件来使其通用化,并评估是否检查了另一个控件,然后相应地应用/删除验证器。

【问题讨论】:

    标签: angular2-forms


    【解决方案1】:

    根据 Angular 的文档,这是不可能的。在 AbstractControl documentation(这是 FormControl 扩展的类)上,没有成员可以检索当前的验证器。

    不幸的是,您唯一的选择是使用两种可用的方法来处理验证器:

    setValidators() // Replaces all validators
    clearValidators() // Removes all validators
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 2018-04-16
      • 2016-05-06
      • 1970-01-01
      • 2016-12-12
      • 2017-09-01
      • 1970-01-01
      • 2019-06-25
      相关资源
      最近更新 更多