Angular6 Material - 使用带有自定义 ErrorStateMatcher 的输入步进器
Angular6 Material - using Stepper with Input with a custom ErrorStateMatcher
这看起来应该非常简单...在步进器中,您正在收集信息,并且您想要确保电子邮件是一封电子邮件。但似乎共享 'form' 标记会导致错误检查器出现问题并且无法正常工作?
进一步澄清...问题似乎实际上在以下标记元素中...
formControlName="emailCtrl"
当我删除此行,并从 .ts (emailCtrl: ['', Validators.required],) 中删除它的同级行时,错误检查开始工作。但是,这意味着步进器无法验证是否需要执行此步骤。
如何确保步进器验证条目并同时确保 ErrorStateMatcher 正常工作?
这是我的组合 HTML...
<mat-step [stepControl]="infoFormGroup">
<form [formGroup]="infoFormGroup">
<ng-template matStepLabel>Profile Information</ng-template>
<div>
<!-- <form class="emailForm"> -->
<mat-form-field class="full-width">
<input matInput placeholder="Username" [formControl]="emailFormControl"
formControlName="emailCtrl"
[errorStateMatcher]="infoMatcher">
<mat-hint>Must be a valid email address</mat-hint>
<mat-error *ngIf="emailFormControl.hasError('email') && !emailFormControl.hasError('required')">
Please enter a valid email address for a username
</mat-error>
<mat-error *ngIf="emailFormControl.hasError('required')">
A username is <strong>required</strong>
</mat-error>
</mat-form-field>
<!-- </form> -->
</div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</form>
</mat-step>
如您所见,我已将电子邮件插槽的嵌套 'form' 注释掉。在测试中,我试过评论但不评论。无论哪种方式,错误检查都无法正常工作。
以下是一些相关的 .ts 片段...
import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ErrorStateMatcher } from '@angular/material/core';
export class Pg2ErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isSubmitted = form && form.submitted;
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
}
}
...
export class Pg2Dialog {
...
emailFormControl = new FormControl('', [
Validators.required,
Validators.email,
]);
infoMatcher = new Pg2ErrorStateMatcher();
...
this.infoFormGroup = this._formBuilder.group({
emailCtrl: ['', Validators.required],
});
我相信我明白了。 ErrorStateMatcher 需要一个命名的表单控件。在本例中,它是 emailFormControl。声明如下...
emailFormControl = new FormControl('', [
Validators.required,
Validators.email,
]);
此外,步进器需要一个命名的表单组,它本身声明了一个新的表单控件。在本例中,它是 emailCtrl。声明如下...
this.infoFormGroup = this._formBuilder.group({
emailCtrl: ['', Validators.required],
});
要让步进器表单控件使用 ErrorStateMatcher 表单控件,只需将方括号放在 .group 分配中并将 emailFormControl 分配给 emailCtrl。像这样...
this.infoFormGroup = this._formBuilder.group({
emailCtrl: this.emailFormControl
});
我在具有类似问题的不同代码部分对此进行了测试,它在两个地方都有效!
这看起来应该非常简单...在步进器中,您正在收集信息,并且您想要确保电子邮件是一封电子邮件。但似乎共享 'form' 标记会导致错误检查器出现问题并且无法正常工作?
进一步澄清...问题似乎实际上在以下标记元素中...
formControlName="emailCtrl"
当我删除此行,并从 .ts (emailCtrl: ['', Validators.required],) 中删除它的同级行时,错误检查开始工作。但是,这意味着步进器无法验证是否需要执行此步骤。
如何确保步进器验证条目并同时确保 ErrorStateMatcher 正常工作?
这是我的组合 HTML...
<mat-step [stepControl]="infoFormGroup">
<form [formGroup]="infoFormGroup">
<ng-template matStepLabel>Profile Information</ng-template>
<div>
<!-- <form class="emailForm"> -->
<mat-form-field class="full-width">
<input matInput placeholder="Username" [formControl]="emailFormControl"
formControlName="emailCtrl"
[errorStateMatcher]="infoMatcher">
<mat-hint>Must be a valid email address</mat-hint>
<mat-error *ngIf="emailFormControl.hasError('email') && !emailFormControl.hasError('required')">
Please enter a valid email address for a username
</mat-error>
<mat-error *ngIf="emailFormControl.hasError('required')">
A username is <strong>required</strong>
</mat-error>
</mat-form-field>
<!-- </form> -->
</div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</form>
</mat-step>
如您所见,我已将电子邮件插槽的嵌套 'form' 注释掉。在测试中,我试过评论但不评论。无论哪种方式,错误检查都无法正常工作。
以下是一些相关的 .ts 片段...
import { FormControl, FormGroupDirective, NgForm, Validators } from '@angular/forms';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ErrorStateMatcher } from '@angular/material/core';
export class Pg2ErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isSubmitted = form && form.submitted;
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
}
}
...
export class Pg2Dialog {
...
emailFormControl = new FormControl('', [
Validators.required,
Validators.email,
]);
infoMatcher = new Pg2ErrorStateMatcher();
...
this.infoFormGroup = this._formBuilder.group({
emailCtrl: ['', Validators.required],
});
我相信我明白了。 ErrorStateMatcher 需要一个命名的表单控件。在本例中,它是 emailFormControl。声明如下...
emailFormControl = new FormControl('', [
Validators.required,
Validators.email,
]);
此外,步进器需要一个命名的表单组,它本身声明了一个新的表单控件。在本例中,它是 emailCtrl。声明如下...
this.infoFormGroup = this._formBuilder.group({
emailCtrl: ['', Validators.required],
});
要让步进器表单控件使用 ErrorStateMatcher 表单控件,只需将方括号放在 .group 分配中并将 emailFormControl 分配给 emailCtrl。像这样...
this.infoFormGroup = this._formBuilder.group({
emailCtrl: this.emailFormControl
});
我在具有类似问题的不同代码部分对此进行了测试,它在两个地方都有效!