Thursday 14 March 2013

Conditional Validation Rules - InfoPath 2010

Using validation rules in InfoPath 2010 we can easily make certain fields become required based on a condition we set within the rule.

The example below requires the 'Employment end date' field only to become required if the user selects 'Fixed term' in the 'Employment type' field.

Basic form with some fields added



As we want to make the 'Employment end date' conditionally required, we need to select that field and click on the 'Manage Rules' icon in the Home ribbon.


Now that the 'Rules' menu is open, click new and select a 'Validation' rule.



Give the rule a name and then click on 'None' under the Condition heading.



In this example our condition is 'If Employment type is equal to Fixed term' then we want the field to be required.

Type 'Required' into the ScreenTip box and this is what will appear as a tip on the form when the field becomes required.


If you published the form and then opened a new item you would find that the field is now conditionally required. However you will not be able to submit using this rule as it is! see below.


'Employment end date' is not required on load.



With 'Employment type' changed to 'Fixed term' the field is now marked as required.

But if you try adding data to the newly required field, you will get a red dashed line and then a validation error should you try to submit the form. The error will be along the lines of "InfoPath cannot submit the form because it contains errors. Errors are marked with either a red asterisk (required fields) or a red, dashed border (invalid values).




There is an easy fix for this, by tweaking our validation rule condition we can avoid this validation error.




Go back to our field validation rule and modify the condition.





We need to tweak our condition to only make the field required if it is also blank.

Publish your form and you will have conditionally required fields with no validation errors!