[英]Custom form validator Angular
I know this question has been asked a bunch of times, but I still can't figure out how to make it work... I'm losing faith, can you help?我知道这个问题已经被问过很多次了,但我仍然不知道如何让它发挥作用......我正在失去信心,你能帮忙吗?
Trying to make a simple "confirm" password validation on my signup form:尝试在我的注册表单上进行简单的“确认”密码验证:
app.ts
应用程序.ts
this.signupForm = this.formBuilder.group({
emailInputControl: new FormControl(null, {
updateOn: "blur",
validators: [
Validators.required,
Validators.email,
Validators.pattern("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$"),
],
}),
passwordInputControl: new FormControl(null, {
updateOn: "blur",
validators: [Validators.required, Validators.minLength(8)],
}),
passwordConfirmInputControl: new FormControl(null, {
updateOn: "blur",
validators: [Validators.required],
}),
validator: this.checkPassword(
"passwordInputControl",
"passwordConfirmInputControl"
),
});
private checkPassword(password: string, passwordConfirm: string) {
return (formGroup: FormGroup) => {
const passwordInput = formGroup.controls[password];
const passwordConfirmationInput = formGroup.controls[passwordConfirm];
if (passwordInput.value !== passwordConfirmationInput.value) {
return passwordConfirmationInput.setErrors({ passwordMismatch: true });
} else {
return passwordConfirmationInput.setErrors(null);
}
};
}
then, my html file is quite basic:然后,我的 html 文件非常基本:
app.html
应用程序.html
<div *ngIf="signupForm.hasError('passwordMismatch') && signupForm.get('passwordConfirmInputControl').touched">
<p>
Pwd don't match
</p>
</div>
Your issue is that you're not targeting the both passwords fields with your validator.您的问题是您没有将验证器的两个密码字段作为目标。 You can set one for both by setting another
formGroup
like that:您可以通过像这样设置另一个
formGroup
来为两者设置一个:
this.signupForm = this.fb.group({
emailInputControl: new FormControl(null, {
validators: [
Validators.required,
Validators.email,
Validators.pattern("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$")
]
}),
password: this.fb.group(
{
passwordInputControl: new FormControl(null, {
validators: [Validators.required, Validators.minLength(8)]
}),
passwordConfirmInputControl: new FormControl(null, {
validators: [Validators.required]
})
},
{
validator: this.checkPassword
}
)
});
Then you can make the following validator function (with some refactoring by removing both parameters):然后你可以制作以下验证器 function(通过删除两个参数进行一些重构):
private checkPassword(fg: FormGroup) {
const pwd1 = fg.get("passwordInputControl");
const pwd2 = fg.get("passwordConfirmInputControl");
console.log("fg", fg);
return pwd1.value === pwd2.value ? null : { passwordMismatch: { value: pwd2.value } };
}
Then in your HTML, you can refactor your condition like that:然后在您的 HTML 中,您可以像这样重构您的条件:
<div *ngIf="signupForm.get('password').hasError('passwordMismatch') && signupForm.get('password.passwordConfirmInputControl').touched">
<p>
Pwd don't match
</p>
</div>
Note you can retrieve a nested formControl property like this:
password.passwordConfirmInputControl
.请注意,您可以像这样检索嵌套的 formControl 属性:
password.passwordConfirmInputControl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.