Happy
Happy

Reputation: 1855

Angular 2 async validator always invalid

I have the following form control with the simplest async validator I could write:

this.aliasCtrl = this._fb.control('', [(control: AbstractControl) => {
  return new Promise(resolve => {
    console.log(this.aliasCtrl);
    resolve(null);
  });
}]);

My form definition is:

this.contactForm = this._fb.group({
  alias: this.aliasCtrl
});

My form control is always invalid. Here's a plunker: http://plnkr.co/edit/vyr48ke7fWEUwrXy43tn?p=preview I'm sure I've miss something but I cannot find what.

Thanks for help.

Upvotes: 0

Views: 1743

Answers (1)

Santanu Biswas
Santanu Biswas

Reputation: 4787

Change the code to:

this.aliasCtrl = this._fb.control('', null, (control: AbstractControl) => {
  return new Promise(resolve => {
    console.log(this.aliasCtrl);
    resolve(null);
  });
});

Pass null or empty array for validators (second parameter) and the async validator function as the third parameter.

Tested... Works!

Upvotes: 9

Related Questions