【问题标题】:Async Validator Throw Expected validator to return Promise or ObservableAsync Validator Throw 预期的验证器返回 Promise 或 Observable
【发布时间】:2017-12-12 14:54:33
【问题描述】:

我已尝试使用密码值确认密码。我已经按照异步验证器标准完成了。但我想知道它不起作用并向我抛出以下错误。请告诉任何人如何解决此错误。

期望验证器返回 Promise 或 Observable。

这是我的代码。

调用验证器:

cPass: ['', Validators.compose([
  Validators.required, 
  Validators.maxLength(32),
  Validators.minLength(10)
]),
  this.validPassword.bind(this)
]

自定义验证功能:

validPassword(control: AbstractControl) {            
  const isEqual = Observable.of(this.password == control.value);
  return isEqual ? { valid : true } : null;         
}

【问题讨论】:

    标签: angular angular2-forms angular-reactive-forms angular-validation


    【解决方案1】:

    错误不言自明:

    期望验证器返回 Promise 或 Observable。

    您在function 中返回object|null

    只需将其更改为:

    validPassword(control: AbstractControl) {
      return observableOf('12345678910' === control.value).pipe(
        map(result => result ? { invalid: true } : null)
      );
    }
    

    STABKBLITZ DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 2019-03-15
      • 2021-11-26
      • 2019-01-22
      • 2021-06-21
      • 2021-05-01
      • 2021-04-11
      相关资源
      最近更新 更多