如何使用clr-control-error显示来自自定义验证器的错误消息?我有一个自定义验证程序来确认密码匹配。其他验证器(例如required,minlength和maxlength)运行良好。我正在使用最新版本的清晰度。
signup.ts
ngOnInit() {
this.createSignUpForm();
}
createSignUpForm() {
this.signUpForm = this.fb.group({
password: ['', [
Validators.required,
Validators.minLength(4),
Validators.maxLength(12)
]],
confirmPassword: ['', Validators.required]
}, { validator: this.passwordMatchValidator });
}
passwordMatchValidator(g: FormGroup) {
return g.get('password').value === g.get('confirmPassword').value ? null
: {'mismatch' : true };
}
signup.html
<clr-password-container>
<label>Password</label>
<input clrPassword formControlName="password" />
<clr-control-error *clrIfError="'required'">This password is required!</clr-control-error>
<clr-control-error *clrIfError="'minlength'">It must be at least 4 characters!</clr-control-error>
<clr-control-error *clrIfError="'maxlength'">It must be less than 12 characters!</clr-control-error>
</clr-password-container>
<clr-password-container>
<label> Confirm Password</label>
<input clrPassword formControlName="confirmPassword"/>
<clr-control-error *clrIfError="'required'">Confirm Password is required!</clr-control-error>
<clr-control-error *clrIfError="'mismatch'">Password Confirmation must match password</clr-control-error>
</clr-password-container>
答案 0 :(得分:1)
您要将验证器置于表单级别而不是表单控件级别,这意味着它将永远不会为该控件触发。我已经将您的示例重新组织为可以在您想要的东西上运行,并且在ConfirmPassword控件上使用了自定义验证器。
https://stackblitz.com/edit/clarity-password-confirm-validator?file=src/app/app.component.html