【问题标题】:Angular Material form validation stops working after first submit. How do you reset the form fields and validators after submission?Angular Material 表单验证在首次提交后停止工作。提交后如何重置表单字段和验证器?
【发布时间】:2021-09-05 14:46:51
【问题描述】:

我的目标是重置表单字段和验证器。在第一次提交表单后,它目前正在清除。但随后您无需验证即可再次提交。 Please see my stackblitz - 正常提交表单,然后将表单字段留空并再次单击“添加”按钮。您会看到表单提交正常,没有错误。那不应该发生。每次单击“添加”按钮时,我都需要验证正常工作。我尝试了以下各种组合均无济于事:

  • .markAsPristine()
  • .markAsUntouched()
  • .clearValidators()
  • 使用表单组
  • 使用 formGroupDirective

HTML

<table style="width: 300px;">
    <thead>
        <tr>
            <th style="text-align: left;">Name</th>
            <th style="text-align: left;">Value</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let data of customData; let i=index;">
            <td>{{ data.name }}</td>
            <td>{{ data.value }}</td>
            <td style="text-align: right;"><button (click)="remove(i)">X</button></td>
        </tr>
    </tbody>
</table>

<br><br>

<form (ngSubmit)="add()">
    <mat-form-field class="full-width">
        <mat-label>Name</mat-label>
        <input matInput required [formControl]="customDataNameControl" />
        <mat-error *ngIf="customDataNameControl.hasError('required')">Required.</mat-error>
    </mat-form-field>
    <br><br>
    <mat-form-field class="full-width">
        <mat-label>Value</mat-label>
        <input matInput required [formControl]="customDataValueControl" />
        <mat-error *ngIf="customDataValueControl.hasError('required')">Required.</mat-error>
    </mat-form-field>
    <br><br>
    <button type="submit">Add</button>
</form>

TS

import { Component, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-item-list',
  templateUrl: './item-list.component.html'
})
export class ItemListComponent implements OnInit {
  customData = [
    { name: 'sample name 1', value: 'sample value 1' },
    { name: 'sample name 2', value: 'sample value 2' },
    { name: 'sample name 3', value: 'sample value 3' }
  ];
  customDataNameControl = new FormControl('', [Validators.required]);
  customDataValueControl = new FormControl('', [Validators.required]);

  constructor() {}

  ngOnInit(): void {}

  // Remove
  remove(index: any) {
    let confirmation = window.confirm('Delete? This cannot be undone!');
    if (confirmation == true) {
      this.customData.splice(index, 1);
    }
  }

  // Add
  add() {
    if (this.customDataNameControl.valid && this.customDataValueControl.valid) {
      let newCD: any = {
        name: this.customDataNameControl.value,
        value: this.customDataValueControl.value
      };
      this.customData.push(newCD);
      this.customDataNameControl.reset();
      this.customDataValueControl.reset();
      this.customDataNameControl.setErrors(null);
      this.customDataValueControl.setErrors(null);
    }
  }
}

【问题讨论】:

标签: angular validation angular-material angular-reactive-forms


【解决方案1】:

您可以使用FormGroupDirective resetForm() 方法。 Live example.

【讨论】:

  • 谢谢,但提交后验证错误仍然存​​在。我的目标是重置表单字段和验证器,以便表单在每次提交后具有初始行为和外观。
  • 对不起,我误解了这个问题。我更新了答案和示例,请查看。另见stackoverflow.com/questions/48216330/…
  • 如果您使用的是 FormGroup,您不能只执行类似 this.formgroup.setErrors(null);
  • 不,删除错误将允许您提交包含空字段的表单。您不想删除错误,我们只想将表单标记为未提交,以便表单字段删除错误指示符。
猜你喜欢
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
相关资源
最近更新 更多