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 }
你需要通知观察者,我认为它应该这样工作:
createValidator: AsyncValidatorFn = control => {
return new Observable<ValidationErrors | null>(observer => {
const result = control.value;
observer.next(result ? null : {isOutsideState: true});
observer.complete();
});
}