0

我在表单中有一个 FormGroup。单击提交按钮时,我需要验证 FormGroup 控件,但它没有触发。TransactionForm 是表单,allocationForm 是 FormGroup。单击添加按钮时,我需要单独验证 FormGroup。必填字段验证未触发。

组件.ts

this.transactionForm = this.fb.group({
  allocationForm: this.fb.group({
    fund: new FormControl(['', Validators.required]),
    amount: new FormControl(['', Validators.required]),
  })
});

html

<form novalidate *ngIf="transactionForm" [formGroup]="transactionForm" (ngSubmit)="submitTransaction()">
  <div class="form-group">
    <label for="name">name:</label>
    <input type="text" class="form-control" formControlName="name">
    <br/>
  </div>
  <fieldset formGroupName="allocationForm">
    <div class="form-group row">
      <label for="amount" class="col-lg-4 col-form-label">    {{labels.percent}}</label>
      <div class="col-lg-8">
        <input name="amount" type="number" class="form-control" placeholder="" min="1" step="1" formControlName="amount" required>
        <div class="form-control-feedback" *ngIf="errors.amount">
          errors.amount
        </div>
      </div>
    </div>
    <div class="form-group row">
      <div class="offset-lg-4 col-lg-8">
        <button type="button" class="btn btn-secondary" class="form-control" name="addButton" type="submit">Add</button>
      </div>
    </div>
  </fieldset>
</form>
4

1 回答 1

0

在您的代码中

     <input name="amount" type="number" class="form-control" placeholder="" min="1" step="1" formControlName="amount" required>

在 div 标记之后,您使用了错误的条件,它应该是 *ngIf="name.errors.<>",例如:*ngIf="amount.errors.min"。希望能帮助到你

于 2017-07-05T15:18:56.213 回答