Extended data validation for the Multiple-Selection List Box in InfoPath 2010
Hi, Frank Mueller from the InfoPath development team here. The Multiple-Selection List Box (MSLB) control was introduced in InfoPath 2007 to enable users to select multiple items from a list when filling out forms. In this post, I will explain the different ways you can restrict and validate the data entered using this control in InfoPath 2010. I will also cover some advanced tips and tricks.
In this post:
First let’s look at a scenario where you may want to use the MSLB control. Say, for example, you are building a form for customers to order your company’s products. To be able to more effectively spend the company’s marketing budget in the future, the marketing department would like to know how your customers found the company’s web site. So you’ve added a Multiple-Selection List Box to gather responses to that question.
Your customers can now select one or more options. If a customer heard about the web site through a channel that is not specified in the Multiple-Selection List Box control, he/she can select the last checkbox and just type in a custom response.
The marketing department is pleased with the new order form, but many customers do not bother responding to the “How did you hear about us?” question. With InfoPath 2010, solving this problem is very easy.
In InfoPath 2010, there are two main ways that form designers can restrict and validate the data entered using the MSLB control.
- Require that at least one item be selected (At least one selection required) (NEW in InfoPath 2010)
- Require that every selected item have a value (Cannot be blank)
Require that at least one item be selected (At least one selection required) (NEW in InfoPath 2010)
New in InfoPath 2010, we can now enforce that users select at least one non-blank item by setting the “At least one selection required” property. In most cases, this is the setting that you will want to use.
When you preview the form, you will notice a red asterisk in the upper right corner of the control, indicating that a selection is required.
The new type of data validation is only applicable to repeating fields that are bound to Multiple-Selection List Box controls.
When setting the property at the field-level (instead of on the control), you will notice that it’s called “Non-empty occurrence required”. This is because the field can be bound to a control other than the MSLB, in which case users do not make selections when using the control.
You may set any repeating field to “Non-empty occurrence required”, but you will only see an effect when the field is bound to a Multiple-Selection List Box control. “Non-empty occurrence required” is not implemented as an XSD constraining facet, but rather as a special Validation Rule that is stored in the manifest.xsf of your form template. This has the benefit that you can set the data validation even if you are designing a form template based on a locked schema.
Require that every selected item have a value (Cannot be blank)
In most cases, the “At least one selection required” property will do you what you need to ensure that users enter the required data.
In InfoPath 2010, we still support the legacy “Cannot be blank” property. When this property is set, it requires that every item that a user selects from the list has a value. It does not however require that users select an item in the first place.
When you preview the form, you will notice a red asterisk, whenever you select the checkbox of the custom value text box. This indicates that if the user wants to select this option, a value has to be typed in the text box before the form can be submitted. Once you type some text in the custom value text box, the asterisk will disappear.
When would I want to use “Cannot be blank” instead of “At least one selection required”?
If you want to ensure that users do not select any item that does not have a value then the “Cannot be blank” property can be used.
In the example below, both “At least one selection required” and “Cannot be blank” are set. The data is not valid because the custom selection is blank.
In the example below, only the “At least one selection required” is set. The data is valid because at least one non-blank item is selected.
Special behavior in customized SharePoint list forms
In a SharePoint list, when you mark a Choice column as required, it requires users to select at least 1 item and for all selected items to have values.
Marking the field as required in SharePoint, has the same effect as setting both the “Cannot be blank” and “At least one selection required” properties in InfoPath. For consistency with SharePoint, when customizing a SharePoint list form in InfoPath 2010, only one data validation option, “Cannot be blank” is available.
Removing Blank Default values
When designing the order form described above, InfoPath will by default insert a blank default value node for each repeating field. As you preview the form you will notice that the custom value text box at the bottom of the Multiple-Selection List Box control will be checked by default.
If you wish there to be no default selection at all in the control, you need to remove the default value node.
Click on the File button
Select Form Options under the Form Information tab
Select the Advanced category in the Form Options dialog
Click on the Edit Default Values … button
Click on the pluses in front of the fields to expand the data source until you can see the repeating field that is bound to the Multiple-Selection List Box control
Uncheck the checkbox in front of the repeating field
Click OK to close both opened dialogs
When you preview your form now, you will notice that the custom value checkbox is not selected by default.