SharePoint 2010 - InfoPath Forms and Rules

In the previous article we saw the basics of InfoPath forms and the integration with SharePoint 2010. In this article we can explore the 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 3 types of Rules:

  • Validation Rule
  • Formatting Rule
  • Actions

Creating the Form

Open InfoPath Designer 2010 and create a new List from the SharePoint List template. Select your website and enter the name of lists as My Rule List.

p1.png

  1. Validation Rule

    A 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 show a message if an invalid email is 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:

    p2.png

    After selecting the item, you will see that the Rule pane comes into the play. You can use the Rule Pane to manage the rules assigned.

    p3.png

    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:

    p4.png
     

  2. Formatting Rule

    Formatting Rules are associated with controls rather than fields. In case we need to format the color of a textbox based on a condition this rule is the appropriate 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:

    p5.png

    Now drag the field to our form:

    p6.png

    Once the form is ready, select the Age Textbox and from the ribbon menu choose menu item Add Rule > Is Greater than > Bad:

    p7.png

    You will get the following dialog to capture the Max value.

    p8.png

    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.

    p9.png

    You will see that the color is changed to red.
     

  3. Actions

    Now we can try with the Action. Actions allow 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:
     

    1. Country of type text
    2. Currency of type text

    When the Country name is India, the Currency must be set to Rupees.

    When the Country name is America, the Currency must be set to Dollars.

    Let us see how to make this work.

    Now you can remove the previous fields, and add the above 2 fields.

    p10.png

    Now click on the Country field and from the ribbon choose Manage Rules. In the Rule pane that appears, click the New Action button.

    A new Rule will be created. Now click on the Condition link:

    p11.png

    A new dialog will appear. Enter the detail as shown below:

    p12.png

    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 dialog that appears, choose the field Currency and set the value to Rupee:

    p13.png

    Now you have added the condition and action for Rule 1.

    Similarly you can add one more rule; check for the Country America and set the Currency field to Dollar.

    Now Save & Publish the form. Inside SharePoint try entering India and press the tab button. You will see that the Currency field is set to Rupee.

    p14.png

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.

References

Summary

In this article we have explored the InfoPath forms with Rules enabled.  In a real-life scenario this is one common feature used by people.