【发布时间】:2019-03-21 11:11:15
【问题描述】:
我正在尝试为我的注册表单创建一个自定义异步验证器,用于检查电子邮件是否已存在。如果电子邮件不存在则后端返回 404,如果存在则返回 200(无法更改此旧代码)。
我找到了一些教程,但没有找到使用最新 rxjs 库的教程。我创建了这个验证类:
export class UniqueEmailValidator {
static createValidator(httpClient: HttpClient, degree: number, wlId: number) {
return (control: AbstractControl) => {
return httpClient.get(`${url}?degree=${degree}&email=${control.value}&wl_id=${wlId}`)
.pipe(
map(
(response: Response) => {
return response.status === 404 ? null : { emailNotUnique: true };
}
)
);
};
}
}
并在我的 ts 文件中创建我正在使用的表单
this.registerForm = this.fb.group({
email: ['', [Validators.required, Validators.email], UniqueEmailValidator.createValidator(
this.httpClient, this.wlService.wl.degree, this.wlService.wl.id)],
xhr 调用已完成并正确返回,但电子邮件的表单控件仍处于待处理状态。关于我做错了什么有什么想法吗?
【问题讨论】:
标签: angular validation asynchronous rxjs angular7