0

I have this 2 rows in 2 tables in angular that has only one submit button and i need help since I can only disable the submit button if the 2 rows in 2 tables are empty. How can i only disable the button if only 1 of the row is empty but the other one is not empty? <button type="submit" class="btn btn-primary float-right" [disabled]="!myForm.valid || myForm.controls.rows.length == 0">Save</button>

ts

this.myForm = this.fb.group({
      rows: this.fb.array([]),
      rows2: this.fb.array([])
    });

html

<form class="form-horizontal" [formGroup]="myForm" (ngSubmit)="onCreate()" >
    <div class="card-block" formArrayName="rows">
    <table>
    <tr *ngFor="let row of myForm.controls.rows.controls; let i = index" [formGroupName]="i">
    ....
    </table>
    </div>

    <div class="card-block" formArrayName="rows2">
    <table>
    <tr *ngFor="let row2 of myForm.controls.rows2.controls; let i2 = index" [formGroupName]="i2">
    ...
    </tr>
    </table>
    </div>
    <button type="submit" class="btn btn-primary float-right" [disabled]="!myForm.valid || myForm.controls.rows.length == 0">Save</button>
</form>

3 Answers3

0

I am not sure is is the best solution but you can create simple flag in controller and function called after fetching data:

yourflag = false;
fun() {
for (let r of rows) {
if(r == '' || r == null) yourflag = true;
}
for (let r of row2s) {
if(r == '' || r == null) yourflag = true;
}
}

and in your button

<button type="submit" class="btn btn-primary float-right" [disabled]="yourFlag || !myForm.valid || myForm.controls.rows.length == 0">Save</button>
Adam A
  • 157
  • 1
  • 18
0
[disable]="myForm.invalid || !myForm.controls.rows.length || !myForm.controls.rows2.length
Christian
  • 2,676
  • 3
  • 15
  • 25
0

This works for me. <button type="submit" class="btn btn-primary float-right" [disabled]="myForm.invalid || !myForm.controls.rows.length && !myForm.controls.rows2.length">Save</button>