Validate in editable grid

Certified Senior Developer

validations: if(
          a!forEach(
            items: ri!grid,
            expression: count(
              fv!item['test}iscorrect']
            ) > 1
          ),
          "Only one fav can be selected",
          null
        ),

Hello All,

I have an editable grid with multiple rows.

I have fav button on each row. I need to validate only one fav is allowed per user. How can I achieve this pls.

Here validation is not working. All my grid data is coming from ri and saving back to ri.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    You're on the right track, but the structure of your forEach statement is nonsensical as-written (you can't put the "count" inside the loop, it won't know what it's even looking for).  My trick is to do the if() statement inside each loop, test for the desired condition, if it's true return 1, otherwise return 0, and then above the forEach loop, just sum the total output.  If more than one "like" is selected (assuming you've structured your "fv!item" correctly, which i can't tell easily), then the sum will result in 2 or greater, and the validation will show.

    validations: {
      if(
        sum(
          a!forEach(
            items: ri!grid,
            expression: if(
              fv!item['test}iscorrect'],  /* gonna have to trust you on this one... */
              1,  /* each "liked" entry in the loop will return a value of 1 */
              0   /* otherwise return a value of zero */
            )
          )
        ) > 1,  /* if the sum is more than one, that implies more than 1 "liked" entry */
        "Only one fav can be selected",
        null()
      )
    }

Reply
  • 0
    Certified Lead Developer

    You're on the right track, but the structure of your forEach statement is nonsensical as-written (you can't put the "count" inside the loop, it won't know what it's even looking for).  My trick is to do the if() statement inside each loop, test for the desired condition, if it's true return 1, otherwise return 0, and then above the forEach loop, just sum the total output.  If more than one "like" is selected (assuming you've structured your "fv!item" correctly, which i can't tell easily), then the sum will result in 2 or greater, and the validation will show.

    validations: {
      if(
        sum(
          a!forEach(
            items: ri!grid,
            expression: if(
              fv!item['test}iscorrect'],  /* gonna have to trust you on this one... */
              1,  /* each "liked" entry in the loop will return a value of 1 */
              0   /* otherwise return a value of zero */
            )
          )
        ) > 1,  /* if the sum is more than one, that implies more than 1 "liked" entry */
        "Only one fav can be selected",
        null()
      )
    }

Children
No Data