0

I want to have two forms on a page, each with their own submit button and validation. However, when I try and submit the 1st form the validation for the 2nd form triggers (and vice versa). Is there a way I can restrict the validation to trigger just for a given form. Here is an extract from my template (other inputs/tags/attributes removed for brevity):

<form #profileForm="ngForm" (ngSubmit)="onSubmit(profileForm)">
  <input name="name" #name="ngModel" [(ngModel)]="user.name" required/>
  <div class="invalid-feedback" *ngIf="profileForm.submitted && name.invalid">
     <p *ngIf="name.errors.required">Name is required</p>
  </div>
  <button type="submit" class="btn">Save</button>
</form>

<form #settingsForm="ngForm" (ngSubmit)="onSubmit(settingsForm)">
  <input name="accountType" #accountType="ngModel" [(ngModel)]="user.accountType" required/>
  <div class="invalid-feedback" *ngIf="settingsForm.submitted && accountType.invalid">
     <p *ngIf="accountType.errors.required">Account Type is required</p>
  </div>
  <button type="submit" class="btn">Update Settings</button>
</form>

I've had a look into ngModelGroup but couldn't see how this would help TBH as I want to specify on the button which form/validation should be triggered.

Any help would be appreciated.

2
  • Seems to be working fine here: stackblitz.com/edit/… Please provide a minimal reproducible example Commented Apr 12, 2022 at 12:37
  • @AT82 so it does (and thanks for the quick response)! There must be something else in my code/markup thats making this happen. As you suggest I'll strip it back to a point where it works or has minimum code to reproduce the issue. BTW I'm using Angular13 Commented Apr 12, 2022 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.