0
votes

I have a FormGroup inside a form. I need to validate the FormGroup controls when I click the submit button, but it is not triggering. TransactionForm is the form and allocationForm is the FormGroup. I need to validate the FormGroup lone when clicking add button ie. required field validation is not triggering.

component.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>
1
Your question is a bit unclear. At first, from where did this errors come from? I believe you want to write transactionForm.get('allocationForm.amount').errors. Also, to trigger the submit function you should change the type of button => <button type="submit">. - developer033

1 Answers

0
votes

In your code at line

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

After this in the div tag you are using wrong condition, It should be *ngIf="name.errors.<>", example: *ngIf="amount.errors.min". Hope it helps