VM Clarity - 无法将自定义验证器与 clr-control-error 一起使用
VM Clarity - Unable to use custom validator with clr-control-error
如何使用 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>
您将验证器放在表单级别而不是表单控件级别,这意味着它永远不会触发该控件。我已将您的示例重新组织成您希望在 confirmPassword 控件上使用自定义验证器的东西。
https://stackblitz.com/edit/clarity-password-confirm-validator?file=src/app/app.component.html
如何使用 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>
您将验证器放在表单级别而不是表单控件级别,这意味着它永远不会触发该控件。我已将您的示例重新组织成您希望在 confirmPassword 控件上使用自定义验证器的东西。
https://stackblitz.com/edit/clarity-password-confirm-validator?file=src/app/app.component.html