Moovendra Dhinesh babu
Moovendra Dhinesh babu

Reputation: 818

Async Validator Throw Expected validator to return Promise or Observable

I have tried to confirm the password with password value. I have done as per Async validator standard. But I am wondering it's not working and throw me the following error. Please tell anyone how to solve this error.

Expected validator to return Promise or Observable.

Here is my code.

Call Validators:

cPass: ['', Validators.compose([
  Validators.required, 
  Validators.maxLength(32),
  Validators.minLength(10)
]),
  this.validPassword.bind(this)
]

Custom Validation funciton:

validPassword(control: AbstractControl) {            
  const isEqual = Observable.of(this.password == control.value);
  return isEqual ? { valid : true } : null;         
}

Upvotes: 10

Views: 19886

Answers (1)

developer033
developer033

Reputation: 24864

The error speaks for itself:

Expected validator to return Promise or Observable.

You're returning object|null in your function.

Just change it to:

validPassword(control: AbstractControl) {
  return observableOf('12345678910' === control.value).pipe(
    map(result => result ? { invalid: true } : null)
  );
}

STABKBLITZ DEMO

Upvotes: 18

Related Questions