【发布时间】:2020-01-02 12:23:47
【问题描述】:
我的 angular-CLI 应用程序中有一个登录组件。我有字段电子邮件和密码。我创建了两个自定义验证 => 一个用于检查用户是否存在,另一个用于检查密码是否与用户匹配。我检查了内置验证器的工作情况,例如必填字段和有效电子邮件。他们工作正常。问题是我的自定义验证器仅在调用提交后才显示错误。 响应式表单不等待自定义异步验证器解决。
这是我的代码:
import {Component, OnInit} from '@angular/core';
import {FormGroup, FormBuilder, Validators} from '@angular/forms';
import {AuthService} from '../auth.service';
import {noUser, pwdMisMatch} from '../validators';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
form: FormGroup;
submitted = false;
returnUrl: string;
constructor(private formBuilder: FormBuilder, public authService: AuthService) {
this.form = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
password: ['', Validators.required],
},
{
validators: [noUser(authService, 'email'), pwdMisMatch(authService, 'email', 'password')]
, updateOn: 'blur'
}
);
}
ngOnInit() {
this.returnUrl = '/dashboard';
this.authService.logout();
}
get f() {
return this.form.controls;
}
onSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.form.invalid) {
return;
} else {
alert('Success');
}
}
}
这是我的自定义验证器文件:
import {FormGroup} from '@angular/forms';
import {AuthResponse, AuthService} from './auth.service';
export function MustMatch(controlName: string, matchingControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName];
if (matchingControl.errors && !matchingControl.errors.mustMatch) {
// return if another validator has already found an error on the matchingControl
return;
}
// set error on matchingControl if validation fails
if (control.value !== matchingControl.value) {
matchingControl.setErrors({ mustMatch: true });
} else {
matchingControl.setErrors(null);
}
};
}
export function userExist(authservice: AuthService, controlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
if (control.errors && !control.errors.CheckUser) {
// return if another validator has already found an error on the matchingControl
return;
}
// set error on matchingControl if validation fails
authservice.checkUser(control.value).subscribe((res: AuthResponse) => {
if (res.ok) {
control.setErrors({ userExist: true });
} else {
control.setErrors(null);
}
});
};
}
export function noUser(authService: AuthService, controlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
if (control.errors && !control.errors.noUser) {
// return if another validator has already found an error on the matchingControl
return;
}
// set error on matchingControl if validation fails
authService.checkUser(control.value).subscribe((res: AuthResponse) => {
if (!res.ok) {
control.setErrors({ noUser: true });
} else {
control.setErrors(null);
}
});
};
}
export function pwdMisMatch(authService: AuthService, controlName: string, secureControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const secureControl = formGroup.controls[secureControlName];
if (control.errors || secureControl.errors && !secureControl.errors.pwdMisMatch) {
// return if another validator has already found an error on the matchingControl
return;
}
// set error on matchingControl if validation fails
authService.verifyPassword(control.value, secureControl.value).subscribe((res: AuthResponse) => {
if (!res.ok) {
secureControl.setErrors({ pwdMisMatch: true });
} else {
control.setErrors(null);
}
});
};
}
我试过这个answer,但问题没有解决。请帮忙。
【问题讨论】:
-
您的意思是在提交表单之前,on submit 会被调用?
-
no no,"this.form.invalid" 的值失败,并在自定义表单验证解决之前显示成功消息。
-
我需要使表单无效,直到自定义验证完成。我认为手动设置值不是一个好方法。
-
有什么方法可以让响应式表单等待自定义验证解决。只有在表单验证完成时才执行提交功能
-
你试过FormGroup的待定属性吗?
标签: angular validation authentication angular-cli angular-reactive-forms