Partager via


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:

Data Validation

Special behavior in SharePoint List Forms

Removing Blank Default Values

Example Usage:

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.

Multi-select list box

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.

Data validation

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.

  1. Require that at least one item be selected (At least one selection required) (NEW in InfoPath 2010)
  2. Require that every selected item have a value (Cannot be blank)

Multi-select list box Properties

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.

Multi-select list box If you select a blank custom value, an asterisk will appear in the top right hand corner of the custom value text box.

Multi-select list box All asterisks will disappear as soon as you select an option that is not blank, or you select the custom value text box and type in a value.

Technical Details

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.

Multi-select list box

Technical Details

When a field in an InfoPath form is set to “Cannot be blank”, a minLength constraining facet is applied to the XSD element, which requires that corresponding XML nodes contain at least one character.

<xsd:element name="field1" type="my:requiredString"/> <xsd:simpleType name="requiredString"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1"/> </xsd:restriction> </xsd:simpleType> This makes sense for text fields, when the form designer wants to ensure that the person filling out the form has to enter data into the field before it can be submitted for further processing.       First Name The Multiple-Selection List Box control is bound to a repeating field in the form’s data source. When the user fills out the form and makes selections in the control, a new XML node is created for each selection made. If you mark the repeating field that the control is bound to as “Cannot be blank”, InfoPath will ensure that all options selected or entered through the custom value textbox are not blank. However, if no selections are made in the control, no XML nodes are created, and hence the constraining facet of the field is not violated, and the user is not forced to make a selection.

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.

Multi-select list box

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.

Multi-select list box

Advanced Information:

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.

SharePoint Column Settings

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.

Multi-select list box

If you wish there to be no default selection at all in the control, you need to remove the default value node.

  1. Click on the File button

  2. Select Form Options under the Form Information tab

  3. Select the Advanced category in the Form Options dialog

  4. Click on the Edit Default Values … button

  5. 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

  6. Uncheck the checkbox in front of the repeating field

    Edit Default Values

  7. 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.

Multi-select list box

Frank Mueller
InfoPath Developer

Comments

  • Anonymous
    October 13, 2010
    Great article, Frank. I found it very helpful.
  • Anonymous
    May 02, 2011
    Thank you so much for this, i've been going round in circles trying to avoid a default value.
  • Anonymous
    May 02, 2012
    Hi, I have a problem that it seems to be impossible to solve. I'm using multiple selection forms with two infopath lists. I have the same options in those fields of the two lists. I've been trying to pass the options selected on one list to another by workflow of Datapolis. I don't know how, but the options are repeated on the destination list. I spoke with datapolis and they said me that is not they problem. The problem is on infopath. I've been searching a long time... maybe the problem is on blank default values of multiple selection fields, but I've checked it and it doesnt work... I don't know what to do... Maybe I can speak with someone from infopath or whatever... Please. Thanks in advance.
  • Anonymous
    December 03, 2013
    Is it possible to have ALL LOOKUP options marked by default ??
  • Anonymous
    January 28, 2014
    Many thanks. Solved my issue of removing default field values.
  • Anonymous
    March 22, 2014
    what if I want a conditional rule for "at least one selection"?
  • Anonymous
    July 08, 2014
    The comment has been removed
  • Anonymous
    January 07, 2015
    Thank you so much..Appreciate your effort,its useful.
  • Anonymous
    July 06, 2015
    The comment has been removed