I have created a validator directive which calls a web.api method that checks if a name already exists.
I can't get the call to fire unless I subscribe to the event. But how do i return an observable when subscribing?
This is my service call:
doesNameAlreadyExist(name: string): Observable<boolean> {
const url = `/exists?name=${name}`;
return this.http.get<boolean>(url, httpOptions).pipe(
debounceTime(2000),
tap(_ => this.log(`checked application name=${name}`)))));
}
This is my directive:
import { Directive, OnInit } from '@angular/core';
import { Validator, AbstractControl, NG_VALIDATORS, ValidationErrors } from '@angular/forms';
//import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs/Rx';
import { Subject } from 'rxjs/Subject';
import { of } from 'rxjs/observable/of';
import { debounceTime, distinctUntilChanged, switchMap, map } from 'rxjs/operators';
import { StringHelper } from '../classes/stringHelper'
import { ApplicationService } from '../services/application.service';
import { Application } from '../classes/application';
@Directive({
selector: '[applicationNameAlreadyExists][ngModel]',
providers: [
{ provide: NG_VALIDATORS, useExisting: ApplicationNameAlreadyExistsValidator, multi: true }
]
})
export class ApplicationNameAlreadyExistsValidator implements Validator, OnInit {
private searchTerms = new Subject<string>();
private nameAlreadyExists$: Observable<ValidationErrors>;
constructor(private applicationService: ApplicationService) { }
validate(c: AbstractControl): Observable<ValidationErrors> {
return this.applicationService
.doesNameAlreadyExist(c.value)
.map(result => (result ? { duplicateEmail: true } : null));
};
}
I have tried every possible combination to get the method to fire without subscribing but it does not. If I subscribe then how to I return the observable?
ANy help would be greatly appreciated.
UPDATE The link provided does help but the error is not present in the errors object. My html is the following:
<input id="applicationName" name="applicationName" class="form-control" required maxlength="100" #applicationName="ngModel" applicationNameAlreadyExists
[(ngModel)]="application.applicationName" />
{{applicationName.errors|json}}
After the method fires I dont have the key in the errors. I only have
{ "__zone_symbol__state": null, "__zone_symbol__value": [] }
How do I access this error?