Properties item. Clicking the Data Validation . . . button displays a dialog box appears that provides two options: Validation and Script (see Script to define business rules when you are developing custom scripts. Scripts are discussed in further detail later in this chapter.
Figure 6-6: Data Validation dialog box.
If you right-click over a data source element to the properties item and click the Validation and Script tab, you get the same result as shown in Figure 6-6. From the dialog box in Figure 6-6, you can click the Add button and another Data Validation dialog box appears (see Figure 6-7). In this box you can define a set of business rules applied to the selected field or control. The first parameter to set is the field to control. By default, the field is the selected one, but you can choose any field available in the document. This feature permits you to define linked validation rules, where one field depends on another.
Figure 6-7: Data Validation dialog box.
The second parameter to define is the statement. The following table provides a list of the statements available in InfoPath.
Statement | Sample |
---|---|
Is equal to | Age == 120, FirstName == “Michelangelo” |
Is not equal to | Age <> 120, FirstName <> “Michelangelo” |
Is less than | Age < 10 |
Is less than or equal to | Age <= 80 |
Is greater than | Age > 20 |
Is greater than or equal to | Age >= 20 |
Is present | FirstName node is present |
Is not present | FirstName node is not present |
Is blank | FirstName == “” |
Is not blank | FirstName <> “” |
Contains | EMail contains “.com” |
Does not contain | EMail doesn’t contain “.com” |
Begins with | FirstName starts with “P” |
Does not begin with | FirstName doesn’t start with “P” |
The third parameter is the condition. This one can vary depending on the field data type and can be either a constant value (e.g., “Michelangelo”, 120, etc.) or another field.
You can also define which kind of alert to show: inline or message-box-based. If you choose the former, you have to define the ScreenTip value; otherwise, you have to define the Message value. If you accept the rules (by clicking OK), the rules will be applied every time the data is updated, and then when you move from one control to another.Note that, as Figure 6-7 states, you can define a set of rules tied by boolean conditions. This feature gives you the chance to create complex rules by adding a set of simple ones.All these rules are reflected in the XSN file, under the xsf:customValidation element. For example, the rule defined in Figure 6-7 will be written as:
<xsf:customValidation>
<xsf:errorCondition match="/mstns:contact" expressionContext="mstns:lastname"
expression="../mstns:firstname">
<xsf:errorMessage type="modeless" shortMessage="If the first name is present,
then insert the last name too"></xsf:errorMessage>
</xsf:errorCondition>
</xsf:customValidation>
If you combine XML Schema validation and design time rules, you have a rich control over the form data. But there are situations where these rules aren’t enough. Then, the last choice you have is to develop your own rules with some scripts.