Angular Reactive Forms: Validate that Nested FormArray is Not Empty and Show mat-error

I am working with Angular’s reactive forms and have a nested structure where I need to validate that a FormArray of files within another FormArray is not empty. Specifically, each groupFiles can have multiple groups, and each group contains a FormArray of files. I need to display a mat-error message if any group’s files array is empty.

My Form Structure

Here’s how I’ve set up my form:

import { FormBuilder, FormGroup, FormArray, Validators } from '@angular/forms';

export class MyComponent {
  serviceRequestForm: FormGroup;

  constructor(private fb: FormBuilder) {
    this.serviceRequestForm = this.fb.group({
      groupFiles: this.fb.array([])
    });
  }

  get groupFiles(): FormArray {
    return this.serviceRequestForm.get('groupFiles') as FormArray;
  }

  addGroupFile(groupFile: any) {
    const fileControls = this.fb.array([
      this.fb.group({
        fileName: ['', Validators.required],
        is_high: [false, Validators.required],
        uploadFileName: '',
        fileType: '',
        fileBase64: '',
      })
    ]);

    const group = this.fb.group({
      groupFileId: [groupFile.ID],
      groupName: [groupFile.DISPLAY_DESCRIPTION, Validators.required],
      orderNo: [groupFile.ORDER_NO, Validators.required],
      files: fileControls
    });

    this.groupFiles.push(group);
  }

  removeGroupFile(index: number) {
    this.groupFiles.removeAt(index);
  }
}

The Template

I am trying to show an error if the files array is empty using mat-error:

<div formArrayName="groupFiles">
  <div *ngFor="let groupFile of groupFiles.controls; let i = index" [formGroupName]="i">
    <span class="col-sm-12">{{ i + 1 }}</span>
    <mat-form-field style="width: 80%" appearance="outline">
      <mat-label>Group Name</mat-label>
      <input matInput type="text" formControlName="groupName" id="groupName" />
      <mat-error *ngIf="groupFile.get('groupName')?.hasError('required')">
        Field is required
      </mat-error>
      <mat-error *ngIf="groupFile.get('files')?.value?.length === 0">
        Files are required for this group.
      </mat-error>
      <button mat-icon-button color="warn" type="button" matSuffix (click)="removeGroupFile(i)">
        <mat-icon>delete</mat-icon>
      </button>
    </mat-form-field>
  </div>
</div>

The Issue

The mat-error for checking if the files array is empty is not working as expected. The length check on files does not seem to trigger the error display correctly. I have tried a few things, but none seem to work:

  • I tried adding a custom validator to check if the files array is empty.

  • I tried using .length === 0 directly in the template.

What I’ve Tried

  1. Custom Validator:
import { AbstractControl, ValidatorFn } from '@angular/forms';

export function filesNotEmptyValidator(): ValidatorFn {
  return (formArray: AbstractControl) => {
    const files = formArray.value;
    return files && files.length > 0 ? null : { filesNotEmpty: true };
  };
}

Applied like this:

const group = this.fb.group({
  groupFileId: [groupFile.ID],
  groupName: [groupFile.DISPLAY_DESCRIPTION, Validators.required],
  orderNo: [groupFile.ORDER_NO, Validators.required],
  files: this.fb.array(fileControls, filesNotEmptyValidator())
});

Template Length Check:

<mat-error *ngIf="groupFile.get('files')?.value?.length === 0">
  Files are required for this group.
</mat-error>

What I Need

I want to validate that the files FormArray is not empty and show an error message using mat-error if it is. How can I correctly implement this validation and ensure the error is displayed?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật