I have tried to call remote validation by using below code
In component file
Validation code
this.formCompany = this.fb.group({
varEmail: [null, Validators.compose([Validators.required, Validators.maxLength(150),this.emailExist.bind(this)])],
}});
function for check email exist or not
emailExist(control: FormControl) {
if(control.value) {
this.services.emailExist(control.value).subscribe(res => {this.checkEmailExist = res;
if(this.checkEmailExist.exist > 0) {
return { 'emailexist': true };
} else {
return null;
}
} );
} else {
return null;
}
}
HTML Code
<div fxLayout="row" fxLayoutWrap="wrap">
<md-input-container class="ml-xs mr-xs" style="width: 60%">
<input mdInput placeholder="Email Address" maxlength="150" [ngModel]="CompanyEdit.varEmail" [formControl]="formCompany.controls['varEmail']">
</md-input-container>
<small *ngIf="formCompany.controls['varEmail'].hasError('required') && formCompany.controls['varEmail'].touched" class="mat-text-warn">Please insert email address.</small>
<small *ngIf="formCompany.controls['varEmail'].hasError('maxlength') && formCompany.controls['varEmail'].touched" class="mat-text-warn">Email address can not exceed 150 characters.</small>
<small *ngIf="formCompany.controls['varEmail'].hasError('emailexist') && formCompany.controls['varEmail'].touched" class="mat-text-warn">Email address already exist.</small>
</div>
This code not working.