Validating a PeopleEditor Control on PostBack

Let’s talk about validation and the PeopleEditor control. There doesn’t seem to be a consensus on how this is supposed to be done so I’ll outline my findings and what eventually worked for me.

I’ll start by explaining what I was attempting to do. I have a PeopleEditor control on a custom application page. My custom application page is located in the _layouts directory. On this page, I also have a submit button that saves my data to a SharePoint list upon submit.

Now, I need to ensure before submit, that the PeopleEditor control contains a valid entry. Blank and invalid values are not permitted.

I first attempted to set the AlllowEmpty property to false. However, that didn’t seem to help. If I clicked submit, the page would post back and then the control would display an error once the page reloaded. At this point, it was too late to be informing the user that there was a problem with their entry, the page had already posted back, so this was not useful.

I also tried setting the ValidationEnabled property to true. That didn’t seem to make any difference either. I am confused as to how these two properties are supposed to work.

My next attempt was to add an ASP.NET required field validator to the page as follows:

1
2
3
<wssawc:PeopleEditor AllowEmpty="false" ValidatorEnabled="true" Width="250px" ID="pePR" runat="server" SelectionSet="User" MultiSelect="false" />
 
<asp:RequiredFieldValidator ID="rfvPR" ControlToValidate="pePR" runat="server" ErrorMessage="Project Requestor (Cannot be blank)" Text="Cannot be blank." ValidationGroup="SubmitForm" Display="Dynamic"></asp:RequiredFieldValidator>

This took care of the client side validation that prevented the user from clicking submit without first entering a value into the control.

However, this did not handle the scenario when a user entered an invalid value in the control and hit submit. In this case, the form will attempt to submit and create the item, but will fail to save the value to the list item field because the value was invalid. An example of an invalid value would be bad data in that field.

To fix this, in my submit method, I added an IF statement to check how many resolved entities there were.

1
2
3
4
5
6
if (pePR.ResolvedEntities.Count > 0) {
    // continue with the submit
}
else {
    // output an error that the user did not enter a valid user
}

If anyone has any feedback, or has had success using the AllowEmpty property, please comment on this post or drop me a note. I’m interested in hearing how others were able to get around this issue.

, ,

6 Responses to “Validating a PeopleEditor Control on PostBack”

  1. spartan42 July 22, 2011 at 10:37 pm #

    When you are submitting the form if you add the code If(!Page.IsValid) return; then the AllowEmpty option should work

  2. shereen July 22, 2011 at 10:57 pm #

    hmmm interesting. ok i’ll give that a try and see how it works. thanks for the feedback! we’ve actually stopped using the people editor control and have build our own auto complete lookups for users. works a lot better, but we do have users who are still using the people editor control, so it would be helpful to resolve this. do you have a post somewhere outlining this?

  3. Mark August 29, 2011 at 1:49 pm #

    Hi Shereen, I was looking at your post cause I have a similar problem to solve, maybe you can guide me…
    Actually what I need, is to add a control in a custom application page, similar to the PeopleEditor but simpler… It is, Having a textbox (that receives an username or a part of it) and a button, what I need to check is, if the input coincides with an username or a part of it in the AD, and displaying a list with all the similar usernames it found. In other words, what I need is just the “Browse” functionality of the PeopleEditor. Is there any way to do this? Thanks.

  4. shereen August 29, 2011 at 4:06 pm #

    Hi Mark,

    Funny enough, this is on my ToDo list for one of our clients. I was able to start on it, but have not yet finished it up. I don’t know of anything out there that can do that, I’ve looked all over. How soon do you need something like this?

  5. Rinat October 31, 2013 at 12:21 pm #

    spartan42’s solution works

Trackbacks/Pingbacks

  1. Links (2/12/2009) « Steve Pietrek - Everything SharePoint - February 12, 2009

    […] Validating a PeopleEditor Control on PostBack […]

Leave a Reply