I have to admit (even though it sounds really geeky) to my new love affair with the contact selector in InfoPath. I've always known it was there and have used it on numerous occasions but did find it a little bit limited. Lately, I've taken the time to actually look at the control properly and I've been pleasantly surprised with what I have been able to achieve. In my next few blogs, I'm going to cover some cool things that I have discovered with the control while at the same time hopefully answering some commonly asked questions for the InfoPath users out there.
This first post tries to answer the question – 'how can I limit the number of names that can be entered into the Contact Selector in my InfoPath form?' I've seen a couple of answers, none that are simple, some that even involve code. I'm no coder (and don't particularly want to be), so here is a simple solution that utilises the 'count' function and some validation conditions… yep, that's right, no code.
There are plenty of blogs out there that describe how to set up the contact selector in your InfoPath form so I won't bother documenting that here. If you need help, check out this blog.
After you have added your contact selector to the form, it will most likely look something like this:
Your data source will look similar to this
Note: Check to make sure your contact selector is working correctly before continuing with this blog.
As this is an expense claim, I only want the user to be able to enter one name into the contact selector. If you play around with the control, you'll find it's not as simple as just adding validation directly to the contact selector. To do this we need to add a couple extra fields to the form's data source.
- Firstly, add 2 new fields to your data source:
- 'EmployeeNameCount' - This field is going to record the number of names that have been entered in the contact selector.
- 'EmployeeName' – This field will perform the validation check.
To add a new field, click on the "myFields" group in your datasource
Then click on 'Add new Field or Group'
Enter a name for your field and then click on Ok when you are done
After you have finished adding both fields, your data source should look similar to this:
Next we need to perform a count on the 'Person' repeating group and record this number in the 'EmployeeNameCount' field.
Double click on the 'EmployeeNameCount' field to open the field's properties
Click on the 'fx' button to insert a function
Insert a count function and point to the 'Person' repeating group in your data source, then click on ok. Your function should look like this:
Click on Ok to save the changes you have made to the 'EmployeeNameCount' field
At this point you might like to test that the EmployeeNameCount field is doing what it's supposed to do. To test, drag the 'EmployeeNameCount' field onto the form canvas.
Preview your form and then enter a couple of names into the contact selector. You should notice that the number in the EmployeeNameCount field corresponds with the number of names entered in the contact selector.
You can now delete the 'EmployeeNameCount' field from the form canvas (but leave the field in the form's data source)
Now we have a field counting the number of names entered in the Contact Selector, it is easy to set up validation to stop the user from entering more than one name. I prefer to set the validation check on the 'EmployeeName' field that we created at the start of this blog versus on the contact selector field itself. There is a good reason for this which I will explain later. But firstly, let's set up the validation rule.
Double click on the 'EmployeeName' field in the data source to open the field's properties.
Click on the validation tab and then click 'Add'
Set the condition "EmployeeNameCount is greater than 1"
In the 'Screen Tip' field enter "Only 1 employee name is allowed. Please remove excess names."
Your validation condition should look like this:
Click on Ok to save the validation and then Ok again to save the changes to the field's properties.
Publish your form, enter 2 names into the Contact Selector and then try to submit.
If you are using a browser enabled form, the validation error message you get should look similar to this:
Earlier on in this blog I said I prefer to put the validation on the 'EmployeeName' field rather than the Contact Selector itself. My reasoning behind this relates to the error message above. Notice that it includes the name of the field that holds the error. 'Employee Name' makes it easy for the user to identify the field with the error. If you place the validation condition on one of the Contact Selector fields such as 'DisplayName' it is not very intuitive.
Another thing that you can do to notify your users of the validation error is to include an error warning next to the contact selector on the form canvas.
On your form canvas, insert a section and in the section enter "* Only 1 employee name is allowed. Please remove excess names."
You might like to highlight the text in red and make it italics so it stands out.
Double click on the section you added to open the group properties
Click on the Display tab and then click 'Conditional Formatting'
Click on 'Add'
Set the condition "EmployeeNameCount is less than or equal to 1"
Tick the 'hide this control' checkbox
The condition should look like this:
Click on Ok 3 times to save the changes
Publish your form, enter 2 or more names into the Contact Selector.
Notice that when you enter more than 1 name the error message is displayed, making it easy for a user to identify the cause of the validation error.
If your form is client based (vs Browser enabled) then it is essential that you put this extra error warning on the form canvas. Validation errors in a client based form do not point the user to the field that is causing the error making it kind of confusing for someone when they are filling out the form.
That's it - you've finished. This method can be used to achieve other things as well. Say you wanted to enforce the user to input at least 2 names into the contact selector…. As long as you have the 'EmployeeNameCount' field, all it involves is setting up the correct validation conditions. Get creative and see what you can come up with.