Schema-based validation in the Data Validation developer sample form
Schema-based data validation in Microsoft Office InfoPath 2003 is determined by the XML Schema associated with a form. Schema-based validation occurs by default whenever a user fills out a form. After the user enters data into a field and moves out of that field, the data is immediately checked against the XML Schema.
In the Data Validation developer sample form, schema-based validation is used to verify that a number falls within a certain range, verify the type of data in a field, verify that a field contains data, and limit the number of rows that can be added to a table.
Note InfoPath supports the creation of only data type and required field constraints in XML Schemas in design mode. However, the use of other kinds of constraints in the schema is supported. To create other kinds of schema-based validation, you must edit your XML Schema files using Microsoft Notepad or some other text editor.
Range checking and data-type validation in the schema
In the Data Validation developer sample form, the XML Schema defines the validNumber field with the float data type and the range of acceptable values as between 1 and 50 (inclusive). The following is the section of the XML Schema in the sample form's schema.xsd file that defines the validNumber field and its validation constraints (restrictions):
<xsd:simpleType name="validNumber">
<xsd:restriction base="xsd:float">
<xsd:maxInclusive value="50"/>
<xsd:minInclusive value="1"/>
</xsd:restriction>
</xsd:simpleType>
If the value entered is not valid, the field is marked with an inline alert. If the user rests the mouse pointer on the field, additional information appears. The user can right-click the field and click Full error description on the shortcut menu to see more detailed information. The user may save the form even if the error is not corrected, but the inline alert will continue to appear each time the form is opened until the error is corrected.
InfoPath provides a set of default error messages that are used when validating data based on an XML Schema. However, InfoPath also provides the capability to override these default error messages with custom error messages that are specific to the type of validation constraints that are used within a given form. To override the default error messages, you must directly modify the form definition (.xsf) file and associate the error message text with a particular field in the schema using an override element with a nested errorMessage element. In the case of the validNumber field in the Data Validation developer sample form, the following override element was added to the .xsf file as a child element of the schemaErrorMessages element:
<xsf:schemaErrorMessages>
<xsf:override match="/sampleData/number">
<xsf:errorMessage
shortMessage="Invalid Number.">
The value entered must be a valid number between 1 and 50.
</xsf:errorMessage>
</xsf:override>
...
</xsf:schemaErrorMessages>
Note Modification of the .xsf file to override the default data validation error messages and add custom data validation error messages is required only with schema-based data validation. When you use custom or script-based validation, you can use the Data Validation dialog box to add custom data validation messages.
If the XML Schema specifies that a particular field in an InfoPath form is required, an error message appears when the user saves or submits the form if that field was left blank. In the Data Validation developer sample form, a nonEmptyString data type is defined in the schema. It is used as the data type for the favoriteColor field in the sample form:
<xsd:simpleType name="nonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
Here, the type nonEmptyString, derived from simpleType, has been made required by setting a minLength value of 1.
Structural validation in the schema
Unlike the other types of data validation available in InfoPath, structural validation prevents the user from making validation errors. Instead of allowing the structure to become invalid (allowing too many rows in a data table, for example) and responding with an error, structural validation disables any action that could create an invalid structure. The most common type of structural constraint in InfoPath occurs in repeating rows and items with schema-defined minimums and maximums.
In the Data Validation developer sample form, the following structural constraint is defined in the XML Schema and is used for the structural repeating table in the form:
<xsd:element name="listA">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="itemA" minOccurs="1" maxOccurs="2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
In this case, the user is not allowed to add more than two listA elements (which is the list of blocks), nor can the user delete the last instance of a listA element. To the user, this constraint requires a table of no more than two and no fewer than one data-bound rows.
Note Custom error messages cannot be implemented when using structural validation in the XML Schema.