I'm writing an asynchronous validator that checks uniqueness of title
. If titles match then the backend throws and error, this should fire the validation.
component.ts:
constructor(
public http:Http,
private formBuilder: FormBuilder) {
this.form = formBuilder.group({
title: ['', Validators.required, uniqueTitle.bind(this)]
});
}
validator.ts
export function uniqueTitle(control: FormControl) : Observable<any> {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return new Observable((obs: any) => {
control
.valueChanges
.debounceTime(4000)
.flatMap(value => this.http.get('http://localhost:3001/api/v1/academic_terms/show_by_title?title=' + control.value, {headers}))
.subscribe(
data => {
obs.next(null);
obs.complete();
},
error => {
let message = error.json().errors;
let reason;
if (message === 'Title Taken') {
reason = 'validateTitle';
}
obs.next({ [reason]: 'Title has already been taken' });
obs.complete();
}
);
});
}
This works fine, but if I quickly type any title with say 10 chars then the debounce will wait (4 seconds in this case ... for testing) and it will then hit the server 10 times.
- A
- AB
- ABC
- etc
Am I missing something here? I read that it might have something to do with the fact that I'm not returning an observable but I can't make sense of it.