I have an issue with my usage of angular 2 async validators.
I have the following component:
@Component({
templateUrl: './send-activation-information.component.html'
})
export class SendActivationInformationComponent implements OnInit {
activationInformationForm: FormGroup;
submitted: boolean = false;
constructor(private userAccountService: UserAccountService,
private formBuilder: FormBuilder,
private router: Router) {
}
ngOnInit() {
this.activationInformationForm = this.formBuilder.group({
email: ['', [
Validators.required,
Validators.pattern(AppConstants.EMAIL_PATTERN)
], [
validateEmailKnownFactory(this.userAccountService),
validateUserAccountNonActivatedFactory(this.userAccountService)
]]
});
}
sendActivationInformation() {
this.submitted = true;
if (this.activationInformationForm.valid) {
this.userAccountService.sendActivationInformation(this.activationInformationForm.value)
.subscribe(() => this.router.navigate(['/sendactivationinformation/success']));
}
}
}
which uses two sync validators and two async validators specified in two arrays.
For some reason the following async validator always indicates a validation error:
export function validateUserAccountNonActivatedFactory(userAccountService: UserAccountService): {[key: string]: any} {
return (control: AbstractControl) => {
return userAccountService.userAccountAlreadyActivated(control.value)
.map(alreadyActivated => alreadyActivated ? {userAccountNonActivatedValidator: {alreadyActivated: true}} : null);
};
}
...whether or not the backend call returns false or true.
FYI, userAccountAlreadyActivated does a http call to the backend as follows:
userAccountAlreadyActivated(email: string) {
let body = 'email=' + email;
return this.http.get(this.urls.USER_ACCOUNT.ALREADY_ACTIVATED + body);
}
trueorfalseis returned from the backend... - balteovar f = validateUserAccountNonActivatedFactory(this.userAccountService); f(someControl).subscribe(val => console.log(val));just for debugging purposes? - Günter Zöchbauer