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

1 Comment

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.

Changing the Namespace for an InfoPath 2010 code-behind project

Leave a comment

After reading this post by the InfoPath blog team on adding an InfoPath project to TFS source control, I went about organizing my form so that I could dump it in source control.

Since the form will eventually incorporate scores of elegant code, I added VSTA code-behind using the handy new Developer ribbon.  I quickly changed the namespace of my code to match our TFS naming standard, saved the project, closed the code, and re-opened it (Alt+Shift+F12) to make sure that everything stuck.  It didn’t:

FUUUUUUUUUUUUUUUUUUUUUUUUUUUU

InfoPath has encountered an error.  The operation failed.  Thank you for the descriptive awesomeness.

I closed the project, snooped around the directory containing the code, and found the %ProjectName%.Designer.xml file.  This contains the following entry:

hostitem:namespace=”%ProjectName%”

where %ProjectName% is the name of my InfoPath project.  I just changed this to the namespace that I assigned to the class library, re-opened the project, and boom, it worked.

To summarize, if you want to change the namespace for an InfoPath code-behind project, do the following:

  1. Change the namespace declared in the code (e.g. namespace Company.Project.Forms.ThisFormName)
  2. Change the namespace specified for the class library by right-clicking the class library, selecting Properties, then specifying the default namespace in the Application tab.
  3. Try to build the project– you will get an error complaining about the namespace.  Double-clicking the error opens the %ProjectName%.Designer.cs file; scroll to the top of this class and modify the namespace to match the namespace of the form code class.
  4. Save and close the code as well as the form (make sure it is published as source files, NOT a .xsn file– for more info, see the InfoPath blog team post that I linked to above).
  5. Navigate to the source code directory and open the %ProjectName%.Designer.xml file.  Find this entry: hostitem:namespace=”%ProjectName%” and change %ProjectName% to the namespace you specified above.
  6. Save, close, and re-open your form.  You should now be able to open the code-behind project.

Follow

Get every new post delivered to your Inbox.