Files
LudosData/src/app/validators/username.validator.ts
2018-04-27 21:06:19 -04:00

38 lines
921 B
TypeScript

import { Injectable } from '@angular/core';
import { FormControl } from '@angular/forms';
import { RegistrationService } from '../registration.service';
@Injectable()
export class UsernameValidator {
debouncer: any;
constructor(
private registrationService: RegistrationService
){ }
checkUsername( control: FormControl ): any{
clearTimeout(this.debouncer);
return new Promise(resolve => {
this.debouncer = setTimeout(() => {
this.registrationService.validateUserName(control.value).subscribe((res) => {
console.log(res.users.length)
if(res.users.length === 0){
resolve(null);
}else{
resolve({'usernameInUse': true});
}
/*
if(res.ok){
resolve(null);
}
}, (err) => {
resolve({'usernameInUse': true});
*/
});
}, 1000);
});
}
}