解决错误后,mat-form-field不会删除mat-form-field-invalid类

穆罕默德·埃纳布(Mohamed Enab)

我正在尝试为创建自定义验证器mat-input一切正常,在发生错误时显示错误,但在解决错误后,mat-form-field不会删除mat-form-field-invalidhttps://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>
达伦·鲁恩(Darren Ruane)

问题在于CrossFieldErrorMatcher,当您真正想要的只是查看该表单的子集时,您正在使用整个表单来验证密码。

要解决此问题,只需FormGroup为passwords元素添加一个单独的名称,以便您的表单构建如下所示:

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]]
})

然后在您的模板中,只需将您的密码元素“包装”在中divFormGroup就可以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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Mat-form-field中删除Mat-form-field-wrapper?

<mat-form-field>的错误。正常输入工作正常,但不能使用mat-select而不会出现错误

mat-form-field必须包含MatFormFieldControl

在触摸控件之前显示mat-form-field错误消息

与mat-form-field中的matSuffix mat-checkbox交互

mat-list-item 重叠中的 mat-form-field

如何调整mat-form-field mat-chip的大小?

如何在mat-form-field元素中的下划线类之间添加空间?

如何删除mat-form-field Angular Material中的[object Object]

正确的方法来填充和删除mat-form-field的边距

如何基于值禁用角度的mat-form-field?

mat-form-field始终显示所需的红色?

在mat-form-field中更改边框颜色

是否可以调整 mat-form-field-outline 的厚度?

mat-form-field必须包含MatFormFieldControl并且已经导入

为什么 mat-form-field 没有出现?

mat-form-field 占位符文本未对齐

angular - 覆盖 mat-form-field 继承的样式

Angular 12- mat-form-field 超出搜索栏

错误错误:mat-form-field必须包含MatFormFieldControl。下拉列表验证

提交后未在表单中捕获mat-form-field中的值

Angular 6错误显示为“ mat-form-field”不是一个已知元素:

面对像“mat-form-field”这样的多个错误不是已知元素并阻止网页加载

Angular Material DatePicker-错误:mat-form-field必须包含MatFormFieldControl

Angular 10:ERROR 错误:mat-form-field 必须包含 MatFormFieldControl

无法使用 css 选择器修改角度材料表单字段类“mat-form-field-outline”

将 <mat-icon> 从一个 <mat-form-field> 更新到另一个不会持续更新图标

“ mat-form-field”和“ mat-input-container”有什么区别

角度指令-ElementRef-无法从mat-form-field中选择mat-select