Angular两步自定义验证

时间:2018-06-14 12:31:47

标签: angular typescript validation

我有电子邮件验证程序(字符串是电子邮件,如果它存在于数据库中):

验证

static IsEmailInDb(authService: AuthService) {
    return control => new Promise((resolve, reject) => {
        authService.isEmailInDb(control.value).subscribe(res => {
            console.log(res);
            if (res) {
                resolve({ EmailUsed: true });
            } else {
                resolve(null);
            }
        });
    });
}

.ts文件:

  this.form = this.fb.group({
  password: new FormControl('', [Validators.required, RegisterValidation.ComplexPassword, Validators.minLength(8)]),
  confirmPassword: new FormControl('', [Validators.required]),
  email: new FormControl('', [Validators.required, Validators.email], [RegisterValidation.IsEmailInDb(authService)]),
}

HTML:

<mat-form-field>
    <input matInput placeholder="Email" formControlName="email">
    <mat-error *ngIf="form.get('email').hasError('email')">{{'EmailValidation' | translate}} </mat-error>
    <mat-error *ngIf="form.get('email').errors?.EmailUsed">{{'EmailUsed' | translate}}</mat-error>      
</mat-form-field>

它工作正常!现在我的问题是:在其他验证通过后,是否有可能在数据库中验证电子邮件?如果字符串甚至不是有效的电子邮件,我不想折磨我的服务和数据库......

0 个答案:

没有答案
相关问题