Hello, i want use a async validator in ionic 2 (angular 2)
.
1. first get authdata - token
2. check validity in async request on server.
import { Injectable } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ApiService } from './../../services/api.service';
import { AuthenticationService }
from './../../services/authentication.service';
@Injectable()
export class Validators{
apiService: ApiService;
authenticationService: AuthenticationService;
constructor(apiService: ApiService, authenticationService:
AuthenticationService) {
this.apiService = apiService;
this.authenticationService = authenticationService;
}
checkUniqueUserEmail(control: FormControl): Promise<any> {
const promise = new Promise<any>(
(resolve, reject) => {
this.authenticationService.getToken()
.then(token => {
let observable =
this.apiService.validateUniqueUserEmail(control.value, token);
resolve({'uniqueUserEmail': true});
observable.subscribe(
(res:any) => {
console.log('it never gets here');
console.log(res.data);
if (res.data && !res.data.valid) {
console.log('invalid');
resolve({'uniqueUserEmail': true});
} else {
console.log('valid');
resolve(null);
}
},
(err) => {
console.log(err);
}
)
})
}
);
return promise;
}
}`
In Formbuilder:
this.registerData = this.formBuilder.group({
email : [
'',
[Validators.required,
Validators.pattern("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[a-zA-Z]{2,4}")],
[(control: FormControl) => {
this.customValidators.checkUniqueUserEmail(control)}
]
],
...
i become this error:
*Runtime Error
Cannot read property 'subscribe' of undefined
Stack
TypeError: Cannot read property 'subscribe' of undefined
at http://localhost:8100/build/main.js:111845:14
at new t (http://localhost:8100/build/polyfills.js:3:11329)
at toPromise (http://localhost:8100/build/main.js:111843:12)
at _convertToPromise (http://localhost:8100/build/main.js:8455:187)
at Array.map (native)
at http://localhost:8100/build/main.js:8448:80
at http://localhost:8100/build/main.js:8461:49
at Array.map (native)
at _executeAsyncValidators (http://localhost:8100/build/main.js:8461:23)
at FormControl.asyncValidator (http://localhost:8100/build/main.js:8448:28)*
THX FOR HELP
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.