iFormBuilder JavaScript Tips & Tricks
What's covered:
- What is a Multi-Paging Subform?
- Why would I want to check for duplicates?
- How do I check for duplicates on my Multi-Paging Subform?
What is a Multi-Paging Subform?
A Multi-Paging Subform allows users to capture as many records as they need for a particular Subform element. For instance, if you are using a Subform for images, setting the subform Link Mode to "Multiple" will allow you take as many or as few photos as needed with each photo creating their own Subform record.
Why would I want to check for duplicates?
There may be times when you are using a Multi-Paging subform and you want to check that a specific field doesn't contain the same value twice within all of the subform records. Below are the steps to ensure a user enters a unique value in each record for the defined element.
How do I check for duplicates on my Multi-Paging Subform?
To check for duplicates, follow the instructions below.
PLEASE NOTE: In the below example, we are operating under the following conditions.
- Parent Form Name: test_duplicates_parent
- Subform Name: test_duplicates_sub
- The data column name of the element on the Subform being checked: my_id
- The data column of the Subform element on the Parent form: my_sub_text
- my_id has a Dynamic Value of my_id=""
In order to perform this operation, you must have selected multiple as the Link Mode for your Subform element as shown above.
STEP 1. On your parent form, add the following code to the Page Level Javascript.
function iformAppendString(myArray, myElement) {
var total = '';
for (var i = 0; i < myArray.length; i++) {
if (total.length > 0)
total += ',';
total += myArray[i][myElement];
}
return total;
}
function hasDuplicates(array) {
var valuesSoFar = Object.create(null);
for (var i = 0; i < array.length; ++i) {
var value = array[i];
if (value in valuesSoFar) {
return true;
}
valuesSoFar[value] = true;
}
return false;
}
More information on this function can be found here.
STEP 2. Click Save.
STEP 3. Add a Text Area element to the parent form with a data column name of my_text_area, and in the Dynamic Value field put the following:
my_text_area = iformAppendString(test_duplicates_parent.my_sub_text, 'my_id')
*Replace the following data column names with the names you created in your forms:
test_duplicates_parent: Parent Form name
my_sub_text: Subform element name on the parent
my_id: Data column name of the element on the Subform being checked
STEP 4. Add a Text Field below the Subform element with a data column name of duplicate_found, and in the Dynamic Value field, put the following:
duplicate_found=hasDuplicates(my_text_area.split(","))
PLEASE NOTE: You can add a Condition Value of false to hide the element from the device.
STEP 5. Add another Text element and check the "Required" box under Element Properties.
STEP 6. Once the Required box has been checked, change the element type to a Label element.
STEP 7. In the Condition Value field of the Label element, put the following:
duplicate_found=="true"
This condition value keeps the required label hidden while all entries are unique. If a duplicate is detected, the label will appear blocking the parent record from being completed.
* If you would like to show the actual name of the value inputted or chosen instead of the array number, you can change the page level javascript from return true to return value:
Along with the above, if you are checking a picklist element on the subform for duplicates, make sure to also change the following on your parent form in the text area dynamic value:
Test the functionality out on your device.
Be sure to download the below form package to use as an example.
Comments
5 comments
Very nice and infomatic post.Thanks for sharing.
http://www.themesrefinery.com/javascript-tips-tricks-best-practices/
I can't get this to work. After loading the provided page level script, the function "my_text_area = iformAppendString(test_duplicates_parent.my_sub_element, 'my_sub_text') " does not seem to be working. Nothing is happening to the text area field.
Also, I can't distinguish what you mean by "Element we want to check duplicates for: my_id" because you also say, "**The my_sub_text is the name of the element in the Subform you want to check duplicates for." It seems to collide.
I followed the instructions to the letter and was not able to get this to function. It needs to be re-written or make a export package of a working example.
Thanks for the heads up. Give us a bit to put together a form package and I'll let you know when it's available, as well as update the article.
Hi, all. The article has been updated as of 10/19/2017. A form package is included above at the end of the how-to section. Be sure to let us know if you have any issues.
Please sign in to leave a comment.