FormControl:异步验证程序未触发以验证无效输入

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

我正在尝试使用Promise实现远程服务器验证器,但是文本输入没有被触发。

`app.component.html'

<form [formGroup]="myForm">
  Foo: <input type="text" formControlName="foo">
  <span *ngIf="!myForm.get('foo').valid">Not valid foo</span>
</form>

'app.component.ts'

ngOnInit() {    
  this.myForm = new FormGroup({
    'foo': new FormControl(null, [Validators.required, this.validateAsync.bind(this)])
  }); 
}

validateEmail(control: FormControl): Promise<any> | Observable<any> {
    const promise = new Promise<any>((resolve) => {
    //post the control.value and check for the response value: fooIsValid
    if (fooIsValid)
        resolve(null); 
    else
        resolve({'FooRuleValidation': true});
    });
    return promise;
}

我做错了什么?我注意到,这使得要求也无法正常工作。

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

您需要将异步自定义函数作为第三个参数传递

尝试一下:

this.myForm = new FormGroup({
    'foo': new FormControl(null, [Validators.required],[this.validateAsync.bind(this)])
  }); 
© www.soinside.com 2019 - 2024. All rights reserved.