Avoid duplicates in repeat group

Hi, I have a repeat group question about children which asks the same set of questions (ID number, age, sex…) about each child. Is it possible to make sure that you can’t enter the same child ID in the same household for 2 different kids?

This is a bit difficult to accomplish in a form when the individual question is asked, since when duplicate inputs are provided the form doesn’t have a way to determine which of the two inputs was the “bad” input.

One way to correct for this is to create a hidden value outside of the repeat group which uses the count() and distinct-values() functions to make a flag that simply signifies that a duplicate input has been provided by the user in the set of ids (roughly: count( /data/repeatgroup/idquestion) != count(distinct-values( /data/repeatgroup/idquestion )), and then display a generic “Each ID must be unique” prompt after the ID question any time this flag is identified. In 99% of cases that would provide the right user experience: Showing a label to the user immediately after entering an duplicated value.

here’s a thought:

building on what Clayton suggested, perhaps if you had a string outside the repeat that joins the id’s with a space character, and inside the repeat, you use the function selected()

that should theoretically work, and it would tell you at the time of entering the ID if you had entered it already rather than having to check all the id’s after being done with the repeat

haven’t tried it, but i imagine it should work as desired


Thanks so much, Clayton! I created a hidden value outside the repeat group called childID_duplicate:
count(#form/childrenRepeat/childID) != count(distinct-values(#form/childrenRepeat/childID)), somehow “data” became “#form” automatically, and a warning label with display condition:
#form/childID_duplicate = true.
But I got the following error when testing:

Calculation Error: Error in calculation for /data/childID_duplicate uses an invalid reference inside a count function

Do you have any idea what could go wrong?

Interesting, it looks like there’s a limitation for the input type of the distinct-values function which requires an underlying selector.

I think you can work around that by switching the second half of the expression to be a space separated list like the following

count-selected(join(" ", distinct-values()))

which should be equivalent to what I suggested