Apache server email exists check with ionic


#1

form validation from apche server using http

this.authForm = fb.group({
‘email’ : [null, Validators.compose([Validators.required, EmailExists.existsEmail ])]

		})

email validator

export class EmailExists {

static existsEmail(fc: FormControl): ValidationResult } {
	
		let headers = new Headers();
		headers.append('Content-Type', 'application/x-www-form-urlencoded');
		let data='email='+fc.value;
		
		http.post('http://localhost/shirtmela/API/registration_email_check.php',data,{headers: headers})
		.map(res => res.json())
		.subscribe(data => {
			
			let posts = data.msg;
			if(posts=='success'){
				return ({existsEmail: true});
			}else{
				return (null);
			}
		}, (err) => {
			
			return (null);
		});
}

}

How can i solve this problem using static method. Thanks in advance.


#2

I would suggest not using a static method. Use a lambda instead:

let emailValidator = (fc: FormControl) => {
  // do stuff. `this` is reliable in here.
};

#3

html

<div class="error-box" *ngIf="authForm.controls['email'].hasError('validEmail') && authForm.controls['email'].touched">* Invalid Email</div>

ts file

import { EmailExists } from '../../providers/email-exists';

let validEmail = (fc: FormControl) => this.emailExists.emailCheck(fc.value);
 'email' : [null, Validators.compose([ Validators.required, Validators.minLength(5), Validators.maxLength(50), validEmail ])]

providers

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class EmailExists {
 data:any;
  constructor(private http: Http) { }

	loadProduct(email:any) {

		return this.http.get('http://localhost/shirtmela/API/registration_email_check.php?email='+email)
		  .map((res: Response) => res.json()).subscribe(
		  res => {
			res ? {} : {"existsEmail": true}
		}, (err) => {
			 null;
		});
	
	}
}

Am i wrong here. Thanks for your reply.


#4

You’re returning a Subscription instead of an Observable. Don’t call subscribe inside of loadProduct().