Skip Ribbon Commands
Skip to main content

Quick Launch

Alana Helbig > Posts > Validate the Contact Selector in InfoPath – Limit the number of names a user can enter into the Contact Selector
July 17
Validate the Contact Selector in InfoPath – Limit the number of names a user can enter into the Contact Selector

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.

  1. 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:

 

 

  1. 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

     

     

  2. 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)

     

  3. 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.

     

     

  4. 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.

     

  5. 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.

     

  6. 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

     

  7. 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.

 

Upcoming posts:

Comments

Re: Validate the Contact Selector in InfoPath – Limit the number of names a user can enter into the Contact Selector

This seems like a great blog however I have run into a problem, which is probably of my own doing.

When I drag and drop the EmployeeNameCount field, it starts off at 1 and when I add contacts to the contact selector it becomes blank.

The ContactSelector is working fine outside of this functionality.  I checked my fields to see if they had default values but nothing.

Any advice?
 on 27/07/2009 11:41 PM

Re: Validate the Contact Selector in InfoPath – Limit the number of names a user can enter into the Contact Selector

This seems like a great blog however I have run into a problem, which is probably of my own doing.

When I drag and drop the EmployeeNameCount field, it starts off at 1 and when I add contacts to the contact selector it becomes blank.

The ContactSelector is working fine outside of this functionality.  I checked my fields to see if they had default values but nothing.

Any advice?
 on 27/07/2009 11:59 PM

EmployeeNameCount defaults to 1

The employee count field does default to 1. This is because there is already 1 row existing in the 'Person' repeating table. In a browser form, if you publish the form, you'll notice that it initially displays as 1 but as soon as you click anywhere in the form it reverts to 0. As you start adding names you should see the field start counting correctly.

In the client (or when previewing the form) and as you say, the field defaults to 1 and stays that way.

If you are using this method to make the field mandatory - Probably a better method, now that I think of it, is on the 'EmployeeName' field set the default value to 'DisplayName' and then set validation on the 'EmployeeName' field so that it cannot be blank. This will work in both the browser and the client. I will try to do a post on this shortly.
Alana
 on 29/07/2009 8:32 AM

Contact Selector allows blank for first person

I'm keen to test this out with my form, however the problem I'd really like to be able to prevent is when the contact selector has a semi colon before the validated name. I cannot reproduce this problem but I have forms (web) submitted with "; lastname, firstname" as the entry. Maning the first name (that the workflow uses) is blank.

Anyone else have this problem?

Using the method above might be confusing for users - as they don't notice the semi colon and believe they have only entered one name.
 on 30/07/2009 3:54 PM

Re: Contact Selector allows blank for first person

You definately raise a good point. I have been meaning to post a blog on how you can set a validation condition to check that the name in the contact selector is valid.... just haven't got round to it yet.... to give you hint - the 'AccountType' field should always be equal to 'user'.

Another suggestions is if this is a common occurance in the *validation warning you put on the form canvas, maybe also provide some tips for the user to help them identify this issue.
Alana
 on 30/07/2009 4:03 PM

Re: Contact Selector allows blank for first person

Thanks Alana

Will have a play with the AccountType validation.

Was hoping this might be fixed in 2010 (being about to limit the contact selector/people picker to only one name) - but not looking that way!
 on 12/08/2009 12:12 PM

Re: Contact Selector allows blank for first person

Thanks Alana

Will have a play with the AccountType validation.

Was hoping this might be fixed in 2010 (being about to limit the contact selector/people picker to only one name) - but not looking that way!
 on 12/08/2009 3:52 PM

Re: Validate the Contact Selector in InfoPath – Limit the number of names a user can enter into the Contact Selector

Even easier technique than the ones described above - fewer fields, less work, instant feedback, works in browser:

1. Create a Section beneath your Contact Selector. Type this text within the section: "You may not choose more than one name. Please try again. You have chosen"
2. Add an EmployeeCount field of count(Person) as described above. Add the field to the Section right next to the word "chosen". Finish the sentence by typing the word "people."
3. Place a Validation rule on the EmployeeCount field: If EmployeeCount > 1 then show error alert "Too many".
4. Place Conditional Formatting on the Section: If EmployeeCount <= 1 then Hide the Control.

When the employee count is two or more the Section will display. When it drops below two the Section will disappear.

If your user field is required, ensure the employee DisplayName field is required. Create another validation Section for that if you feel you must.
 on 17/08/2009 5:51 AM

Re: Validate the Contact Selector in InfoPath – Limit the number of names a user can enter into the Contact Selector

Additional note regarding the above tip: Make sure your EmployeeCount field is read-only, so users can't change it when they see the value.
 on 17/08/2009 5:52 AM

Feedback regarding the 'easier technique'

Thank you for posting your version of this technique. It's great to get other suggestions and methods for achieving these outcomes. I must say however, with the method I have posted above, the validation warning is placed on the 'EmployeeName' field. A key reason for having this field is so that when / if the user tries to submit and they get the validation error it actually points them directly to the 'EmployeeName' field (see step 4 of this post). If you put the validation warning on the the 'EmployeeCount' field then the validation error will show this field in the warning and this can sometimes confuse users.... even if you do have a section on your form that specifies the error.
Alana
 on 17/08/2009 8:20 AM