异步验证器反应形式

问题描述 投票:0回答:1


payerGeneralInformationValidators = this.formBuilder.group({
        'payerName': ['', [Validators.required, Validators.pattern('[a-zA-Z 0-9`~!@#$%^&,.\'\"*()_+-]*$'), Validators.maxLength(256), this.createValidator()]],
        'aliasName': ['', []],
        'address1': ['', []],
        'address2': ['', []],
        'city': ['', [Validators.pattern('[a-zA-Z 0-9`~!@#$%^&,.\'\"*()_+-]*$')]],
        'state': ['', []],
        'zip': ['', [Validators.pattern(/^\d{5}(-\d{4})?$/)]],
    });

createValidator(): AsyncValidatorFn {
        return (control: AbstractControl): Observable<ValidationErrors> => {
          return of(true)
            .pipe(
              map((result: boolean) =>
                result ? null : { isOutsideState: true }
              )
            );
        };
      }

我将(true)放置为模拟可观察值,以使测试更容易

我正在尝试验证 payerName,但 CreateValidtor() 函数永远不会返回 null 或 { isOutsideState: true }

angular angular-reactive-forms asyncvalidators
1个回答
0
投票

你需要通知观察者,我认为它应该这样工作:

createValidator: AsyncValidatorFn = control => {
    return new Observable<ValidationErrors | null>(observer => {
        const result = control.value;
        observer.next(result ? null : {isOutsideState: true});
        observer.complete();
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.