How to use form validation with api calls to check if data is correct? Whenever I enter this signup page I got errors like:
Cannot read property 'put' of undefined
Cannot read property 'http' of undefined
HTML:
<ion-content padding>
<p *ngIf="submitAttempt" style="color: #ea6153;">Please fill out all details accurately.</p>
<ion-list inset>
<form [formGroup]="signupForm">
<ion-item>
<ion-input formControlName="userName" [(ngModel)]="userName" placeholder="UserName" type="text"></ion-input>
</ion-item>
<ion-item>
<ion-input formControlName="email" [(ngModel)]="email" placeholder="Email" type="email"></ion-input>
</ion-item>
<ion-item>
<ion-input formControlName="password" [(ngModel)]="password" placeholder="Password" type="password"></ion-input>
</ion-item>
</form>
<button ion-button block (click)="register()">Register</button>
</ion-list>
</ion-content>
TS:
this.signupForm = formBuilder.group({
userName: ['', Validators.compose([Validators.required, Validators.pattern('[a-zA-Z]*')]), this.authService.checkUserName],
email: ['', this.authService.checkEmail],
password: ['']
});
AUTH:
checkUserName(control: FormControl): any {
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.put('http://localhost:63203/api/Customers/CheckIfUserExist', JSON.stringify(control.value), { headers: headers })
.subscribe((res) => {
resolve(res);
}, (err) => {
reject(err);
});
});
}
checkEmail looks same just with different api address.