0

I'm trying to implement a remote-server-validator using a Promise, but the text input does not being triggered.

`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)])
  }); 
}

validateAsync(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;
}

What I'm doing wrong? I've noticed that it makes the required not to work as well.

0

1 Answer 1

1

You need to pass async validator function as a third argument

Try this:

this.myForm = new FormGroup({
    'foo': new FormControl(null, [Validators.required],[this.validateAsync.bind(this)])
 }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.