javascript - 解决错误后,mat-form-field 不会删除 mat-form-field-invalid 类
问题描述
我正在尝试为mat-input
. 一切正常,发生错误时会显示错误,但解决错误后mat-form-field
不会删除mat-form-field-invalid
类
https://imgur.com/a/MSsaVop
class CrossFieldErrorMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective |
NgForm | null): boolean {
return control.dirty && form.invalid;
}
}
errorMatcher = new CrossFieldErrorMatcher();
register = this.fb.group({
userName: ['', [Validators.required]],
userEmail: ['', [Validators.required, Validators.email]],
phone: ['', [Validators.required, this.phoneNumberValidator]],
userPassword: ['', [Validators.required]],
passwordconfirm: ['', ],
adminName: ['', [Validators.required]],
adminPassword: ['', [Validators.required]],
}, {
validator: this.passwordValidator
})
passwordValidator(form: FormGroup) {
const condition = form.get('passwordconfirm').value !==
form.get('userPassword').value;
return condition ? { passwordsDoNotMatch: true } : null;
}
<mat-form-field class=" my-4 ">
<input matInput type="password" required
formControlName="userPassword" placeholder="Password"
[type]="hide ?'password' : 'text'" class="col-4" name="password">
</mat-form-field>
<mat-form-field class=" my-4 ">
<input matInput type="password" required
formControlName="passwordconfirm" placeholder="Confirm
Password"
[type]="hide2 ? 'password' : 'text'" class="col-4"
[errorStateMatcher]="errorMatcher" name="conpassword">
<mat-error *ngIf="register.hasError('passwordsDoNotMatch')">
Passwords do not match!
</mat-error>
</mat-form-field>
解决方案
问题是您CrossFieldErrorMatcher
正在使用整个表单来验证密码,而您真正想要的只是查看该表单的子集。
要解决这个问题,只需为密码元素添加一个单独FormGroup
的元素,这样您的表单构建将如下所示:
register = this.fb.group({
userName: ['', [Validators.required]],
userEmail: ['', [Validators.required, Validators.email]],
phone: ['', [Validators.required, this.phoneNumberValidator]],
// Create a separate FormGroup here to isolate the scope
passwords: this.fb.group({
userPassword: ['', [Validators.required]],
passwordconfirm: [''],
}, { validator: this.passwordValidator }),
adminName: ['', [Validators.required]],
adminPassword: ['', [Validators.required]]
})
然后在您的模板中,只需将您的密码元素“包装”在 a 中,并将其div
指定为新的,如下所示:FormGroup
FormGroup
<div [formGroup]="register.get('passwords')"> // Assign the new FormGroup
<mat-form-field class=" my-4 ">
<input matInput type="password" required
formControlName="userPassword" placeholder="Password"
[type]="hide ?'password' : 'text'" class="col-4" name="password">
</mat-form-field>
<mat-form-field class=" my-4 ">
<input matInput type="password" required
formControlName="passwordconfirm" placeholder="Confirm Password"
[type]="hide2 ? 'password' : 'text'" class="col-4"
[errorStateMatcher]="errorMatcher" name="conpassword">
<mat-error *ngIf="register.get('passwords').hasError('passwordsDoNotMatch')">
Passwords do not match!
</mat-error>
</mat-form-field>
</div>
推荐阅读
- php - 在收到的 html 中获取页面内容 curl 错误 404
- alfresco - 如何重组 Alfresco 存储库
- javascript - 无法让 chartjs 用动态数据绘制图表(变量未硬编码)
- javascript - 替换多个包含 $ 符号的字符串
- autohotkey - 绘图窗口中的 AutoCAD(也是 LT)组合框控件,ChooseString 找不到打印机
- c# - 从堆栈中投射对象
- python - 地图在 DatetimeIndex 上表现异常
- android - 具有视图组合的 RecyclerView
- java - 安装失败并显示消息错误:android.os.ParcelableException:java.io.IOException:仅请求内部,但空间不足
- python - 在 Tkinter 中使用相同按钮的启动和停止功能