これは非常に簡単なように思えます...ステッパー内で情報を収集していて、電子メールが電子メールであることを確認したいとします。しかし、共有された '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>
ご覧のとおり、メール スロットのネストされた「フォーム」をコメント アウトしました。テストでは、コメントアウトせずにコメントしてみました。いずれにせよ、エラーチェックは正しく機能しません。
関連する .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],
});