<mat-form-field class="example-full-width">
<input matInput placeholder="Re-Enter password" formControlName="reEnterpwdFormControl">
<mat-error
*ngIf="signupForm.get('password.pwdFormControl').hasError('mismatch')">
password did not match
</mat-error>
</mat-form-field>
,密码验证器如下:
export function passwordMatchValidator(g: FormGroup) {
return g.get('pwdFormControl').value === g.get('reEnterpwdFormControl').value
? null : {'mismatch': true};
}
和组件中的formGroup验证器按如下方式完成:
password:new FormGroup({
pwdFormControl : new FormControl('', [
Validators.required,
Validators.pattern(/^(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*])[\w!@#$%^&*]{8,20}$/),
]),
reEnterpwdFormControl : new FormControl('',[
Validators.required
]),
},this.passwordMatchValidator)
问题是: 会调用验证程序函数并返回正确的true和false,但返回{'mismatch':true}时不会显示错误消息。