I am trying to create a control with async validation and I don't want sync validation:
Creating form using formBuilder.
this.myForm = this.fb.group({
someControl: new FormControl(
"",
null,
[this.valueUnique.bind(this)]
)})
If I keep sync validation parameter as Validators.required then I get no error.
Error:
ERROR Error: Expected validator to return Promise or Observable. at toObservable (forms.js:603) at Array.map () at FormControl.asyncValidator (forms.js:591) at FormControl.push../node_modules/@angular/forms/fesm5/forms.js.AbstractControl._runAsyncValidator (forms.js:2535) at FormControl.push../node_modules/@angular/forms/fesm5/forms.js.AbstractControl.updateValueAndValidity (forms.js:2508) at FormControl.push../node_modules/@angular/forms/fesm5/forms.js.AbstractControl._updateTreeValidity (forms.js:2523) at forms.js:2522 at forms.js:3309 at Array.forEach () at FormGroup.push../node_modules/@angular/forms/fesm5/forms.js.FormGroup._forEachChild (forms.js:3309)
Validator function:
/**
* Validator to checking existance/uniqueness
* of entered value
* @param control
*/
valueIsUnique(control: AbstractControl): Promise<ValidationErrors|null> | null {
if (control && (control.value !== null && control.value !== undefined)) {
return new Promise((resolve, reject) => {
this.service.checkValueExists(control.value).subscribe(res => {
{
if (res && res['data']) {
resolve({
unique: true
});
}
else {
resolve(null);
}
}
},
err=>
{
resolve(null);
});
});
}
}
this.valueUnique.bind(this)is returning anObservableorPromise? Because hat is what the error says, your binding to the functionvalueUniqueis not returning this type. - Sam Vloeberghs