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
Add the saveInto in a local variable which would validate if it is null then the button will be disabled. If not they can use the button. Please share the code snippet for an accurate answer.
If this field is a boolean, you can just use the sum() function as true equals 1 and false equals 0. As there might be some null values, use something like the following code snippet.
a!localVariables( local!values: {1,0,null}, sum(local!values = true) )
Apply this to the field in your items where you store the favourite value. Then, disable your button, in case the sum is > 1.
use if( count(where({<favButton_gridData>})) >1,false,true) in the disable parameter of favButton field ( considering favButton as boolean type)
Thank you Stefen. But my requirement is to validate in editable gridlayout in validation section. My code snippet pasted.
Thank you . But my requirement is to validate in editable gridlayout in validation section. My code snippet pasted.
OK, and that is exactly what my example is about. Just adapt it to your data.
yes in that case you can try this count() part and throw validation in validation parameter. Use the column name directly instead of fv!item. since fv!item won't have scope in validation parameter
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() ) }