Yesterday I introduced the Multi-Select List Box and talked about how its data source is somewhat special: it binds to a repeating list of fields instead of a single field like the normal List Box does.
When you need to ensure that at least one item is selected in the multi-select list box, you may be tempted to go to Muti-Select Listbox Properties to check the "cannot be blank" property; however, this property will not be available for multi-select list boxes because of their binding. Since a multi-select list box binds to a repeating field, what you really need is something that means "occurs at least once". Here's how…
Visuals - Showing the red asterisk (*)
- Insert a multi-select list box and remove its border.
- Create a two-column layout table; place the multi-select list box into the first column of that table.
- Place an expression box into the second column of the layout table. Use static text * (asterisk) as the contents of the expression box and set its font color to red.
- Set up conditional formatting on the expression box:
if count of elements in the repeating field is greater than 1, hide this control
- Set up a visible outside border on the layout table.
The steps provided above will provide a visual indication to the user that the field is required - a red asterisk will be shown whenever the multi-select list box doesn't have any option checked. After you're done, you should see results similar to the following:
We still need to make sure that our users can't submit a form with this error - since nothing we've done above prevents form submission. This can be achieved in several ways:
- Option 1: Use rules for form submission
As the first step of submission, check if count of elements in the repeating field is less than 1. In that case, show an error message and stop processing all rules, essentially preventing form submission.
- Option 2: Add data validation to another field
if count of elements in the repeating field is less than 1. This will cause InfoPath to add an error to its Errors Collection behind the scenes if the condition is not met, and your users will not be able to submit the form.
- Option 3: Write code for the Submit action
In code you can check the condition and add elements to the Errors Collection as necessary.
- Alex Weinstein