【问题标题】:I have two forms on a page but submit button is working on both validations?我在一个页面上有两个表单,但提交按钮对两个验证都有效?
【发布时间】:2021-09-20 12:58:36
【问题描述】:

我有两个带有一个提交按钮的表单。这里我的问题是,我对两种表单都进行了验证。但我希望这些验证单独工作,就像当我提交 clusterForm 时,它应该验证并且应该发生后调用。但直到我填写其他表格才发生。这是我的 ts 代码。

  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 表单:及其控件

   <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>

【问题讨论】:

  • productFG提交时要调用哪个方法
  • 两种表单的调用后方法相同,但是当我提交集群表单时,它要求我填写我不想填写的 productfg 表单。

标签: angular validation


【解决方案1】:

试试下面的代码

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

【讨论】:

  • 谢谢@Navnath
猜你喜欢
  • 2013-01-14
  • 1970-01-01
  • 2016-07-31
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多