Infopath is a member of Microsoft Office software family. Its main function is to collect XML data for clients and generate xml files according to the expected format. It collects data in the form of visual forms, it also supports C # programming interfaces and external data sources. The use of infopath avoids end users from generating XML data files in full handwriting mode, and can also unify the XML file format, which is widely used in Moss systems and website development.
There are several specific steps to develop an XML front-end form page using infopath, such as compiling an XML sample file, generating an XML schema file, importing the primary data source in infopath, and designing infopath form rendering, if necessary, you can add a C # event to the form.Code, Such as the action to be executed when the form is opened or saved, and the action to be executed when an additional button is attached. If you are interested in infopath development, you can study it separately. This article mainly describes how to verify the data validity in infopath.
In infopath, there are three ways to verify the data validity:
1. Use the built-in data type of infopath.
2. Add custom data verification conditions or verification events to the input item.
3. Add Rules or verify events to customize the form button for Data Validity verification.
Use infopath built-in data types for automatic data verification
This is the simplest data verification method. When you add a control to infopath or specify a control for the domain in the primary data source, you can specify a data type for the control input. If a separate primary data source is not specified for infopath, infopath constructs the data source structure according to the control you added in the form. If the primary data source is specified before the form editing, the data type accepted by the control depends on the Data Type of the domain in the data source to which it is bound. The data type of the domain in the data source can be specified separately when compiling the XML schema file. If the data type is not specified, the default value is 'string', indicating that any data type is acceptable.
In infopath, right-click the control to set the Data Validity verification, click "properties", or double-click the control. In the displayed dialog box, set the Data Type of the control, for example: infopath supports the following data types:
- Text (string)
- Integer)
- Decimal (double)
- Boolean (true/false)
- Hyperlink (anyuri)
- Date)
- Time)
- Date and time (datetime)
In the Properties dialog box, you can also set the default data values and data formats. After the data type of the control is set, if the data type of the end user does not match when entering the form, infopath will give the corresponding prompt information, such:
Use custom data verification conditions or verification events for Data Validity Verification
You can set custom data validation conditions in infopath or write custom events for data validation. This requires additional programming overhead or computer programming knowledge.
1. Use custom data verification Conditions
Right-click the control to Set Data Validity verification, click "properties", or double-click the control. In the displayed dialog box, click "data verification, in the pop-up dialog box, click "add". In the data verification dialog box, set the data verification conditions, enter the on-screen prompt information when data verification fails, and the message content to be displayed in the warning dialog box.
Infopath has already integrated a lot of data verification conditions for users to choose directly, such as the value in the field is greater than, less than, equal to, contains, does not contain, and so on. If you want to define your own matching mode, you can select "Match mode" or "Mismatch mode", select "select mode" from the third drop-down list, and write a custom mode Regular Expression in the pop-up dialog box. For example, if you want to stipulate that this field can only accept digits containing the decimal point, you can write the following regular expressions:
-? [ 0 - 9 ] * \. ? [ 0 - 9 ] +
Then set the warning information:
2. Use custom verification events
To use a custom verification event, you must write C # code to verify the data validity. Because infopath has a built-in vsta programming interface, this allows us to easily compile C # code for infopath Through Visual Studio to complete some specific functions. Note that when writing this article, my local development environment is Visual Studio 2008 + Office 2007. When installing Office 2007, you must manually check the vsta option under infopath, otherwise, infopath does not support vsta programming interfaces by default. In infopath, vsta uses the development environment of Visual Studio 2005 by default. You do not need to install Visual Studio 2005. By default, vstaProgramAnd environment configuration.
Right-click the control to Set Data Validity verification and choose program> validating event. At this time, infopath will contact Visual Studio to open the vsta project. In the vsta project, we can add custom methods for events. Note that infopath uses VB by default.. NET language to create a vsta project. If you want to change the project language, click "Tools"-"options" and select the language as C # On the "design" tab #, you can also set the default storage location of the project. If you have already created VB. for vsta projects in the. NET language, you can use "Tools"-"form options" to delete code from the "programming" tab, and then create a new vsta project, you can also select the storage location of the vsta project in this infopath form.
We add the following code in vsta to enable us to accept only numeric data verification results in the above example.
Public Void Field1_validating ( Object Sender, xmlvalidatingeventargs E)
{
If ( ! E. undoredo && E. Operation = Xmloperation. valuechange)
{
// Obtain the field value in the data source.
Xpathnavigator Root = Maindatasource. createnavigator ();
String Field1 = Root. selectsinglenode ( " // My: field1 " , Namespacemanager). value;
// check whether the user input matches the regular expression
system. text. regularexpressions. regEx = New system. text. regularexpressions. regEx ( @ " -? [0-9] * \.? [0-9] + " );
If(!RegEx. ismatch (field1 ))
{
E. reporterror (E. site,True,"Only numbers are allowed");
}
}
}
Save the code and close Visual Studio, save or publish the compiled infopath form, open the form instance directly, and enter the content in the domain to be verified, when the verification fails, infopath prompts you according to the requirements in the verification Event code.
Use Rules or verification events on the button for Data Validity Verification
In infopath, in addition to the two methods described above, we can also set rules for the buttons on the form and verify events to verify the data validity of the domain.
1. Use Rules to verify data Validity
For example, the form contains a text box, an expression box, and a button. We want to verify whether the value entered by the user in the text box is a number and display the information in the expression box. Follow these steps to add a rule to the button:
-
- Right-click the button and click "properties", or double-click the button. In the displayed dialog box, click "rules.
-
- In the "Rules" dialog box, click "add ".
-
- In the "Rules" dialog box, click "add operation ".
-
- In the "operation" dialog box, select "set Domain value" from the "operation" drop-down list ".
-
- Click the button next to "domain". In the displayed dialog box, select the domain bound to the expression box and click "OK" to close the dialog box.
-
- Leave the "value" text box blank in the "operation" dialog box, click "OK" to close it, and then close the "rule" dialog box.
-
- At this time, a new rule has been added to the "Rules" dialog box, which is used to clear the values in the expression box when the button is clicked. Add a rule according to the previous steps, bind it to the expression box, and set the value to "only allowed numbers ".
- Set conditions for Rule 2. In the "condition" dialog box, select the domain to be verified (the field corresponding to the text box), and select "Mismatch mode ", set the matching regular expression-?[0-9]*\.?[0-9] +
-
- Confirm and close all dialog boxes
When a user opens a form instance, enter a non-number in the text box, and click the button. A prompt is displayed in the expression box. If the content entered by the user complies with the verification rules, the content in the expression box is cleared.
2. Use the verification event on the button to verify the Data Validity
Right-click the button and click "properties", or double-click the button. In the "properties" dialog box, click "Edit form code". A new button event has been added to the vsta project code, add the following code:
Public Void Ctrl2_5_clicked ( Object Sender, clickedeventargs E)
{
// Obtain reference from the source root node of form data
Xpathnavigator Root = Maindatasource. createnavigator ();
//Clear the content in the expression box.
Root. selectsinglenode ("My: field2", Namespacemanager). setvalue ("");
// Check whether the value in the text box is a number and the prompt information is displayed in the expression box.
String Field1 = Root. selectsinglenode ( " My: field1 " , Namespacemanager). value;
System. Text. regularexpressions. RegEx = New System. Text. regularexpressions. RegEx ( @" -? [0-9] * \.? [0-9] + " );
If(!RegEx. ismatch (field1 ))
{
Root. selectsinglenode ("// My: field2", Namespacemanager). setvalue ("Only numbers are allowed");
}
}
Note: In infopath, only the corresponding prompt information is displayed for all data verification. You can force save the form and ignore the prompt information for Data Validity verification. In addition, the name of the master data source node used in the C # code in this article (for example, My: field1 and my: field2) varies depending on the actual situation.