In the previous article we have seen the basics of InfoPath forms and the integration with SharePoint 2010. In this article we can explore InfoPath Forms with Rules.
What are Rules?
A rule in InfoPath is an Action which takes place on an Event. An event could be a field value change. There could be multiple Actions for an Event.
Types of Rules
Please note that there are three types of Rules:
- Validation Rule
- Formatting Rule
Creating the Form
Open InfoPath Designer 2010 and create a new List from SharePoint List template. Select your website and enter the name of list as My Rule List.
1. Validation Rule
Validation rule validates a field value and displays an error message. We can use range comparison, email validations etc. using this rule.
We are trying to add a Validation rule for:
- Email field which will Validate itself and shows a message if Invalid email entered
Use the Add field link from the left pane to create a new field named Email. Once the field is added select the field and from the ribbon menu click
the Add Rule button. Then from the menu select the option Is Not an Email Address > ShowValidation Error.
After selecting the item, you can see that the Rule pane comes into the play. You can use the Rule Pane to manage the rules assigned.
Now Drag and Drop the Email field to the form. Save and Publish it.
Inside the SharePoint site try to enter an email which is invalid and you should get an error as shown below:
2. Formatting Rule
Formatting rule are associated with controls rather than fields. In case if we need to format the color of a textbox based on a condition this rule is the apt one.
In this scenario we are adding a Field named Age. If the Age is greater than 100, the textbox should turn red in color. Let us do it.
Remove the previous field from the Form and add a new field named Age. Set the type of field to Number as shown below:
Now drag the field to our form.
Once the form is ready, select the Age Textbox and from the ribbon menu choose menu item Add Rule > Is Greater than > Bad
You will get the following dialog to capture the Max value.
Enter 100 as the value and click the OK button.
Good! Now you are read with the Formatting Rule. Save & Publish the form and inside SharePoint try to add a value greater than 100.
You can see that the color changed to red.
Now we can try with the Action. Action allow to perform the following:
- Query Data from other Source
- Set another Field Value
- Submit the Form
- Get Data from a Web Part
Please note that there could be more than one action associated with a field.
Our scenario is to have 2 fields:
- Country of type text
- Currency of type text
When the Country name is India, the Currency had to be set to Rupee.
When the Country name is America, the Currency had to be set to Dollar.
Let us see how to make this work:
Now you can remove the previous fields, and add the above two fields.
Now click on the Country field and from the ribbon choose Manage Rules. In the appearing Rule pane click the New Action button.
A new Rule will be created. Now click on the Condition link:
A new dialog will be appearing. Enter the detail as shown below:
So this adds our first condition, when the Country field is equal to India. Now we have to set the Action for it.
Click on the Add button from the Rules pane and choose Set a field’s value. In the appearing dialog, choose the field Currency and set the value to Rupee.
Now you have added the condition and action for Rule 1.
Similarly you can add one more rule, check for Country is America and set the Currency field to Dollar.
Now Save & Publish the form. Inside SharePoint try entering India and press the tab button. You can see that the Currency field is set to Rupee.
So this concludes our Actions rule.
From the Rules pane, you can delete / disable a rule based on the requirement. The changes are reflected in just a Save and Publish.
In this article we have explored InfoPath forms with Rules enabled. In a real life scenario, this is one common feature used by people.