InfoPath 2010 Validation in Repeating Tables – Check for a Duplicate of a Single Value

The problem:

I have a repeating table that is populated by code and contains a boolean value.  The boolean value represents whether or not the row is “selected.”  I want a user to be able to set ONLY ONE of the boolean values to true and thus “select” only one row; if more than one row is selected, a validation error should occur.  The highlighted field below is type boolean.

You’re probably thinking to yourself, isn’t that the purpose of radio buttons? Yes, it is: give the user multiple choices amongst which only one can be selected.  But there is no way to place a single radio button in a repeating table and allow it to be selected in only ONE row of the repeating table.  This is because the radio button can’t be bound to the repeating group.

The solution: Use an expression

Add validation to the VendorQueryDataSelected field using an expression.

Before I get into the specifics, I found this forum post where someone has a problem similar to mine, except they are attempting to weed out duplicate values for a single field within the repeating table.  I don’t care if there are duplicate values; in fact, if I implemented the method described there, the validation would throw an error because the default value of “false” for the boolean field is duplicated for every row in the table.

Instead, I only care if the value “true” is duplicated.  Here is the expression I used for the validation:

count(/my:InvoiceFields/my:Vendor/my:VendorQuery/

           my:VendorQueryData[my:VendorQueryDataSelected = “true”]) > 1

This reads IF (the COUNT OF (WHERE the value of the field VendorQueryDataSelected == “true”)) is greater than 1, then throw a validation error.

Or, generically:

count(/my:RootGroup/my:SubGroup/my:SubSubGroup[my:FieldToCheck = “true”]) > 1

Result: Below is my table with a single vendor selected:

Now, I select two vendors.  The validation event triggers:

Boom.

Advertisements

1 thought on “InfoPath 2010 Validation in Repeating Tables – Check for a Duplicate of a Single Value”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s