フォームがあり、サーバー側のエラーをこのように処理します。
constructor(private store: Store<fromRoot.State>) {
this.containerTypes$ = store.pipe(select(fromWelcome.getCts));
this.errors$ = store.pipe(select(fromModal.getError) as any);
this.errors$.subscribe(errors => {
if (errors) {
if (errors.username) {
this.usernameError = errors.username;
this.regForm.controls['username'].setErrors({usernameServerError: this.usernameError});
// <-------- I want to call my mat-error here
}
}
}
);
this.regForm = new FormGroup({
username: new FormControl('', [Validators.required, Validators.minLength(5)]),
email: new FormControl('', [Validators.required, Validators.email]),
password: new FormControl('', [Validators.required, Validators.minLength(8)])
});
.htmlで私はこれを行います:
<mat-error *ngIf="regForm.controls['username'].hasError('usernameServerError')"> {{usernameError}} </mat-error>
残念ながら、送信しようとするとエラーが発生し、入力の下に表示されません。最初に入力をクリックするか、もう一度フォームを送信する必要があります。フォームでエラーを受け取った直後にエラーを表示するにはどうすればよいですか?
フォームを有効にする必要がありました
constructor(private store: Store<fromRoot.State>) {
this.containerTypes$ = store.pipe(select(fromWelcome.getCts));
this.errors$ = store.pipe(select(fromModal.getError) as any);
this.errors$.subscribe(errors => {
if (errors) {
this.regForm.enable() // <---- here
if (errors.username) {
this.usernameError = errors.username;
this.regForm.controls['username'].setErrors({usernameServerError: this.usernameError});
}
}
}
);
this.regForm = new FormGroup({
username: new FormControl('', [Validators.required, Validators.minLength(5)]),
email: new FormControl('', [Validators.required, Validators.email]),
password: new FormControl('', [Validators.required, Validators.minLength(8)])
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加