【发布时间】:2018-11-16 14:45:54
【问题描述】:
我正在使用 Angular Material 对用户进行身份验证。我目前正在尝试在确认密码与第一个输入的密码不匹配时显示正确的 mat-error。
这是我的html:
<mat-form-field hintLabel="Minimum 8 Characters" class="">
<mat-label>Password</mat-label>
<input
matInput
#input
type="password"
formControlName="password">
<mat-hint align="end">{{input.value?.length || 0}}/8</mat-hint>
</mat-form-field>
<mat-form-field>
<mat-label>Confirm</mat-label>
<input
matInput
required
type="password"
#confirm
formControlName="confirmPassword">
<mat-error *ngIf="form.get('confirmPassword').invalid || confirmPasswordMismatch">Password does not match</mat-error>
</mat-form-field>
一旦用户专注于密码确认并且不输入任何内容而取消关注,就会显示该错误。一旦用户输入任何内容,即使确认与密码不匹配,错误也会消失。
这是我的 TS 文件:
public get confirmPasswordMismatch() {
return (this.form.get('password').dirty || this.form.get('confirmPassword').dirty) && this.form.hasError('confirmedDoesNotMatch');
}
this.form = new FormGroup({
userName: new FormControl(null, [Validators.required]),
fullName: new FormControl(null, [Validators.required]),
email: new FormControl(null, [Validators.required, Validators.pattern(this.EMAIL_REGEX)]),
password: new FormControl(null),
confirmPassword: new FormControl(null, ),
}, (form: FormGroup) => passwordValidator.validate(form));
想要的效果是当用户在确认 pw 为空时将文本输入到 pw 输入时显示错误,并且当两者都有文本但确认与 pw 不匹配时显示错误。
【问题讨论】:
标签: javascript html angular typescript angular-material