A mutually exclusive radio button in a repeating section - Jimmy Rishe

InfoPath Dev

Jimmy Rishe

A mutually exclusive radio button in a repeating section

Here's a nifty trick you can use when you want to add a radio button or checkbox to a repeating section or table that can only be checked in one row of the section or table at any given time.

The Scenario
You are creating a form for a team roster for teams in a sports tournament.  As a rule, each team may only designate one team captain and one vice-captain.  You could enforce this using custom validation, but let's see if we can't do something a bit fancier.

Creating the form
We begin by dragging an empty repeating section into the form:

By default, this section will be created as my:group2.
We then add the fields that we want below my:group2.  We create a my:name field to store each team member's name (this will not serve a real purpose in this demo, but let's include it for good measure), a my:captain field and a my:vice-captain field (let's create both of these fields as Boolean (true/false) fields.

Then we drag the fields into the repeating section from the taskpane.  First we drag the name field in and create it as a text box.  Then we drag the captain field with the right mouse button and create it as a checkbox, and drag the vice-captain field with the right mouse button and create it as a radio (option) button.  (Ordinarily you would probably just use one or the other, but for the sake of demonstration we'll use one of each this time.
Creating the radio button should create a checked and unchecked radio button with the words Yes and No next to them.  Delete the No radio button and change the "Yes" text to "Vice-captain."

The final result should look like this:

Adding rules
Now that everything's laid out, it's time to add rules to make the fields mutually exclusive.  Right-click the checkbox (the captain field) and select Rules... top open up the Rules dialog box, and then click Add... to add a new rule, and name it "Clear other captains".
Click Set condition... to create a condition and set the condition to be captain is equal to TRUE. That is, whenever a user clicks this checkbox to designate a team member as the captain, we want the rule to clear all of the other captain fields.


Now add the rule action.  Make the action "Set a field's value."  Select the captain field itself as the field to set, and for the value, just type false.

Please repeat this process for the vice-captain field, replacing captain with vice-captain in the instructions above.

Not done yet
If you preview the form at this point, you will find that you are unable to check any of the checkboxes or radio buttons. This is to be expected.  As soon as you try to set a value to TRUE, the rule is setting all of the nodes of that field to false, including the field you just checked.  To get around this, we'll need to employ a little trick that Hilary Stoupa blogged here.

Editing the manifest
Save the form as its source files, close InfoPath and locate the place where you saved the files.  Open the manifest.xsf file in a text editor of your choice.

In the xsf:ruleSets section, you should find the definitions for your rules:

For the first rule, edit the targetField attribute to have the value:

(../preceding-sibling::my:group2 | ../following-sibling::my:group2)/my:captain

This will tell InfoPath to set the "false" value to the my:captain field in all preceding and following repeating nodes.

Now do the same for the vice-captain rule, replacing my:captain with my:vice-captain.

Save the manifest.xsf file, right-click it in Internet Explorer and click Design to open it up in Design mode again.

Now preview the form.  If you've done everything right up to this point, you should find that any time you click a captain checkbox, all of the other captain checkboxes become cleared, and the same happens for the vice-captain radio buttons.



Alec Pojidaev said:

You might be intersted to look at my alternative solution. To compare ...


August 26, 2009 8:47 AM

hemendraagrawal said:

Hi Jimmy,

I have implement same and it is working fine in client but it is not working with browser based form.

Is there any change to make available for browser based form?

August 6, 2012 2:03 AM
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.