posted on February 20
Just wanted to share this on here. I had to write javascript code to verify that the end user does not add an item to a collection more than once on the modern designer. I also used show hide rules to show the error based on a number field defaulted to 0.
function validateUniqueness() {
//fieldID for the variable that needs to be validated as unique in the collection/table.
let items = LFForm.getFieldValues({ fieldId: 101 });
let len;
let newItem;
let existingItems;
//confirms field being validated is an array.
if (Array.isArray(items)) {
len = items.length-1;
newItem = items[len];
existingItems = items.slice(0, len);
//validates the field and updates the necessary form variables.
if (existingItems.includes(newItem)) {
//fieldID for the variable that triggers the error custom html field.
LFForm.setFieldValues({fieldId: 106, index: len}, 1);
//fieldID for the collection/table
LFForm.changeFieldSettings( {fieldId: 100}, {addButtonLabel: " "} );
}
else {
//fieldID for the variable that triggers the error custom html field.
LFForm.setFieldValues({fieldId: 106, index: len}, 0);
//fieldID for the collection/table
LFForm.changeFieldSettings( {fieldId: 100}, {addButtonLabel: "+ Add"} );
}
}
}
//fieldID for the variable that needs to be validated as unique in the collection/table.
LFForm.onFieldChange(validateUniqueness, { fieldId: 101 });
Here is an example of what the collection looks like when a duplicate item is entered, it also makes it so that they can't add another row until they select a different item because it sets the tables add field to just a blank.
Just change the field ids in the code to your field ids in your form. Hope this is useful to others.
7
0