Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;         
}
like image 264
Kevin - Dhinesh babu Avatar asked Jul 08 '17 11:07

Kevin - Dhinesh babu


1 Answers

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

like image 143
developer033 Avatar answered Sep 23 '22 12:09

developer033