Validation in InfoPathPosted: 2012-01-29 | |
Due to a discussion in a thread at Microsoft Forum, I decided to create a post about a couple of ways to handle validation in InfoPath.
The two ways I will show you are these:
- Disable a button until all questions are answered.
- Showing a message if a question is missed.
This is the how the form looks like.
As you can see we have a couple of questions to answer and then we can move on to the next view to continue with more questions. To stop the user from being able to go to the next view we could disable the button, this is how.
First click on the button Next view, and then on Manage Rules in the ribbon.
Now for the button Next view create a Formatting rule called DisableButton, that looks like this.
Now the button can’t be used until Question1 and Question2 is answered. Use Prereview to make sure it works.
Ok, so now lets move over of how to show a message if the user missed to answer a question. First you have to delete the rule from the previous example.
These are the fields in the form.
First of all, above the button I have placed the field called Message, and it is formatted like this.
No border, read only and with the same color as the rest of the cell, this way it’s invisible until a message is written to it.
As before, choose Manage Rules for the button Next view, this time create two Action rules.
The first rule I name Message_Blank and this rule looks like this.
As you can see I use the action Set a field’s value and the only thing we do here is to set the field Message to nothing/blank. You should not add the apostrophes, InfoPath will do that for you, just leave the field blank.
Now we need to create one more rule to show the message. Let’s call it Message_Error, it’s the same kind of rule with a few differences and it looks like this.
As you can see we I have a condition, the message is not blank, but actually holds the text we would like to show the user. And last but not least, I have check marked Don’t run remaining rules… That is because the last rule this button should have is to switch view, but not if this rules condition is met.
This is how it will look like if the user trys to move on before all questions are answered.
Try it in preview to make sure you done it the right way.