InfoPath Tip: InfoPath Wizards Part 2 - Conditional Formatting Is Your Friend
In my last post, I talked about how to create an online wizard using InfoPath and Forms Services. It was a pretty simple process, but I did mention there was a pitfall regarding data validation.
The pitfall is when using data validation on multiple views in a wizard scenario, the submit button is typically on the last view/page of the wizard. The submit button is responsible for causing data validation to fire. This can create an issue if one of the controls you used 3 pages back has a validation problem. The user is told validation failed, but what they aren’t told is which view contains the offending field.
One way to address this issue is by using conditional formatting.
Conditional formatting allows you to hide or disable controls based on conditions in the form, hence the name. Additionally, there are other formatting options such as read-only or text formatting options.
This approach works best when dealing with situations where the data validation pertains to required fields. You may be able to handle more complex situations, but it will take some creativity and a combination of rules and conditional formatting. For the purpose of this article, I am going to stick to required fields.
Conditional formatting doesn’t actually validate the data, but what it can do is prevent the user from advancing in the wizard if they haven’t filled in all the required fields. The trick is you don’t apply the formatting rules to the field; you apply the formatting to the navigation button.
In our wizard scenario, to advance from step to step (change from one IP view to the next) we had to provide the user with a NEXT button. What we are going to do is set the conditional formatting to disable the NEXT button if our field we are requiring has been left blank.
First, locate the navigation button on your view, right click and choose “conditional formatting.” You will be presented with a conditional formatting dialogue box. You have the option of adding multiple formatting rules.
Next, click the add button and you will be presented with the “Conditional Format” window. The first line in the window is where you will define the condition. For this example,I am going to assume I have a field called “name” that I am going to require.In the conditional format window, I would select the “name” field from the first drop down box. The middle drop down is your comparison argument. For this scenario, I am going to select “Is blank”. The third drop down box becomes unavailable since the comparison argument I chose doesn’t’ make a comparison against another field. At this point,you could also click the “And” button which would allow you to add another condition rule to check if you had more than one field required. For example, you could say if Name is blank OR address is blank. Once you click the “And” button, you are given a drop down list so that you may specify the logic that needs to be applied to the second condition.
The bottom half of the window is what sort of formatting isapplied based on the conditions we just defined above. Since I am checking to see if the field is blank because I am going to require that field (in this case the “name” field), I will check “Disable this control.” The NEXT button will remain on our page, but the user will be unable to click it unless they have provided a value for the name field.
These steps would need to be repeated on the navigation buttons on the rest of the views in our wizard. These steps could even be applied to the submit button on the last view, to prevent submission of the form if certain fields on that page are blank.
The goal of this approach is to prevent the user from advancing to the next page in the wizard if all the fields we need have not been filled out, so refrain from creating conditional formatting rules that reference fields on other views as we are only interested in checking to see if this step in our wizard have been fully completed before allowing the user to advance to the next step.
Granted there are situations where this approach will not work because the validation required is more complex, but works great for scenarios where the need is simply to check if the field has been completed or not.