Skip to main content
July 28, 2022
Question

Need help comparing current index item to previous index item in a foreach loop

  • July 28, 2022
  • 2 replies
  • 0 views

Hello All,

I'm currently creating an editable grid for school records with a field called year graduated. I want the following entries for this field to be compared with previous row to check if the year is greater than the previous row and output an error message. However, I can't seem to find a way to access the previous item. I tried using index function but I'm having a cannot index type error.

a!integerField(
    label: "Graduation Year/Expected Year",
    labelPosition: "ABOVE",
    value: fv!item.yearGraduated,
    saveInto: fv!item.yearGraduated,
    required: true,
    validations: {
        if(
            fv!index >= 2,
            if(
                fv!item.yearGraduated > index(fv!item.yearGraduated, fv!index - 1),
                "Please enter a valid year",
                ""
                ),
                ""
            )
    }
)

Any help would be appreciated. 

2 replies

July 28, 2022

I was able to solve it. I used the rule input instead since I append the rows to rule input each time I add one.

index(ri!educationalBackground.yearGraduated, fv!index - 1)

harshitb6843
July 28, 2022

That is correct. But please remember to add the 3rd parameter in the function (default value) so that the function does not break when the index is not available. Also, please add a check so this condition does not run for the first item in the list.