Actually I stuck in this problem.
I've created a custom async validator to check if a username ist taken or not. This is where I bind it:
'name': ['',[Validators.required, Validators.minLength(4), Validators.minLength(4)], this.asyncName.bind(this)],
And this is my Validator:
asyncName(control: FormControl) {
this.names$ = this.af.database.list('/usernames')
.map (name => {
name.filter(name => {
if (name.name.toLowerCase() === control.value.toLowerCase()) {
console.log("Username taken");
return {
valid: false
};
} else {
console.log("Username available");
return {
valid: true
};
}
})
});
return this.names$;
};
So this validator works. If the user name is taken it logs "Username taken" else "Username available".
My Problem: The return statement is not working. When I subscribe to the state of the form it would be still "Pending" and not "Valid" or "Invalid". Angular2 thinks the Validation is still running.
Does anyone know how to create valid return or resolve statement based on observables?
Maybe to subscribe by my self and do something with the resolve() function. But all my attempts were not working.
Thanks in advance!!!