Home > database >  I have two forms on a page but submit button is working on both validations?
I have two forms on a page but submit button is working on both validations?

Time:09-22

I have two forms with one submit button. Here my problem is, I put validation for both forms.But i wanted those validations work seperately, Like When i submit clusterForm, it should validate and post call should happen. but its not happening till i fill the other form. Here is my ts code.

  sendclusteridApi() {
    this.submitted = true;
    if (this.clusterForm.invalid) {
      return;
    } else if (this.productFG.invalid) {
      return;
    }
    const request = this.createRequest();

    this.projectclosterservice.postStoryStatus(request).subscribe(
      (res: any) => {
        this.filtering = res["1tool-gitlab-pipeline"].data.filtering;
        this.clusterForm.reset();
        this.formData.reset();
      },
      (error) => {}
    );
  }

Html:clusterform


    <div *ngIf="submitted && fclusterForm.clusternames.errors" class="error-feedback error">
      <p *ngIf="fclusterForm.clusternames.errors?.required" class="text-danger ">
        Please select Cluster
      </p>
    </div>

productFG form: and its controls

   <div *ngIf="submitted && isTouchedAndRequired(i, key)" class="error-feedback error">
     <p class="text-danger">{{key | titlecase}} is required.</p>
   </div>


   <div class="d-flex justify-content-end">
     <button (click)="sendclusteridApi()" class="btn btn-primary px-3 py-2">Save</button>
   </div>

CodePudding user response:

Try below code

if(this.clusterForm.invalid || (this.checkedIDs.length!=0 && this.productFG.invalid)){
    this.errorMsg="Please fill all form entries. ";
    return false;
  }
  • Related