轉:InfoPath 中,如何限制 Contact Selector 控制項中輸入的使用者數量? Validate the Contact Selector in InfoPath – Limit the

來源:互聯網
上載者:User

 

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."Make the contact selector mandatory

    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:

  • Use the contact selector to obtain other user details such as department, office and manager from the Profile Database
  • Validate the name entered in the contact selector
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.