Validating Row Count Between Two Form Grids

Introduction

This section illustrates the process of comparing the row counts of two form grids within the same form. This comparison is valuable when ensuring that the row count in one grid (Form Grid 1) meets specific criteria with the row count in another grid (Form Grid 2). 

How does it work?

To compare the row counts of two form grids within a single form, follow these steps:

  1. Import necessary classes and define the validate method.
  2. Use the FormUtil.findElement method to get Form Grid 1 using its ID from the same form.
  3. Extract rows from both grids and compare their counts.
  4. Ensure that Form Grid 1 has a row count that is the same as or higher than that of Form Grid 2.
  5. The method returns true if the condition is met; otherwise, it returns false.

The following Java code is deployed in a multi-row bean shell validator in Form Grid 2 (ID: "formgrid2"). Below is an example code snippet demonstrating how to implement the comparison of form grid row counts:

import java.util.Arrays;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.Form;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.service.FormUtil;
import org.joget.apps.form.dao.FormDataDao;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.plugin.base.PluginManager;
import org.joget.commons.util.LogUtil;
import java.util.List;

public boolean validate(Element element, FormData formData, FormRowSet fg2Rows) {
    boolean result = false;

    // Retrieve the first form grid using its ID
    String fg1Id = "formgrid1"; // Insert the actual Form Grid 1 field ID
    Form form = FormUtil.findRootForm(element);
    Element fg1 = FormUtil.findElement(fg1Id, form, formData);

    if (fg1 != null) {
        FormRowSet fg1Rows = fg1.formatData(formData);
        int fg1RowCount = fg1Rows.size();
        int fg2RowCount = fg2Rows.size();

        // Validate that Form Grid 1 has at least as many rows as Form Grid 2
        if (fg1RowCount >= fg2RowCount) {
            result = true;
        }
    }

    return result;
}

// Execute the validation
return validate(element, formData, rows);
 
Created by Julieth Last modified by Aadrian on Dec 13, 2024