locked
Validate a People Picker control RRS feed

  • Question

  • Hi,

    I would like to validate a People Editor field. I have tried using the property Allow Empty, but unfortunatly it doesnt work. I have used a Required field validator. Luckily it works to some extent. But the problem starts if there is any postback happening on the page. After the postback, it takes the value to be empty even if there is an entry in the control.

    Could anyone help me if there is any perticular property to be enabled or any other solution to acheive the validations on People Editor Control
    Raj
    Thursday, July 24, 2008 1:38 AM

Answers

  • I had the exact same problem on a custom application page.

    I solved it with a Custom Field Validator using client side validation

    <script language="javascript">  
        function CheckProjectManager(source, arguments) {  
            if (aspnetForm.ctl00_PlaceHolderMain_ProjectManager_downlevelTextBox.value == "")  
                arguments.IsValid = false;  
            else  
                arguments.IsValid = true;  
        }  
    </script> 

                    <SharePoint:PeopleEditor   
                    ID="ProjectManager"   
                    AllowEmpty="false"   
                    ValidatorEnabled="true"   
                    MultiSelect="false" 
                    runat="server"   
                    SelectionSet="User" 
                    Width="200px"   
                    TabIndex="2" />&nbsp;  
                    <asp:CustomValidator  
                    ID="rfvProjectManager" 
                    runat="server" 
                    ControlToValidate="" 
                    ErrorMessage="Required" 
                    Enabled="true"   
                    ClientValidationFunction="CheckProjectManager">  
                    </asp:CustomValidator> 
     


    Note that the ControlToValidate on the CustomValidator is set to ""

    The ID for the People Editor  ctl00_PlaceHolderMain_ProjectManager_downlevelTextBox was found using IE Developer Toolbar, Note that downlevelTextBox contains the validated userid e.g. DOMAIN\USERID after pressing check user in the peopleeditor.

    /Jan Lange
    • Edited by Jan Lange Thursday, November 13, 2008 8:57 AM
    • Marked as answer by Matt Burnett Friday, December 26, 2008 5:20 PM
    Thursday, November 13, 2008 8:50 AM

All replies

  • Raj,

    This is how I do it and it works just fine:

    In ASPX file:

    1  
    2  
    3 <SharePoint:PeopleEditor ID="PeopleEditorSubmitter" runat="server" ValidatorEnabled="true" AllowEmpty="false" SingleLine="true" MultiSelect="false" SelectionSet="User" MaximumEntities="1" /> 
    4  
    5  


    In CS file:


    1  
    2         protected PeopleEditor PeopleEditorSubmitter;  
    3  
    4         protected void ButtonSubmit_Click(object sender, EventArgs e)  
    5         {  
    6             if (Page.IsValid)  
    7             {  
    8                 if (PeopleEditorSubmitter.Entities.Count == 0)  
    9                 {  
    10                     // no people found  
    11                 }  
    12  
    13                 // do something useful here  
    14  
    15                 SPUtility.TransferToSuccessPage("Completed Operation.");  
    16                   
    17             }  
    18         }   
    19  

    Hopefully this helps.

    Thanks,

    Paul Liebrand
    http://liebrand.wordpress.com
    http://www.wsswiki.com
    • Edited by Paul Liebrand Thursday, July 24, 2008 1:46 PM ASPX portion was not showing. Edited for readability.
    • Marked as answer by Michael Washam - MSFT Thursday, July 24, 2008 1:57 PM
    • Unmarked as answer by Ganti Friday, July 25, 2008 1:07 AM
    Thursday, July 24, 2008 1:45 PM
  • Hi Paul,

    Thanks a lot for the reply. I have directly used the required field validator, but it doesnt work if the page post back happens. I have also tried working with the page.IsValid option, but havent got the best results.

    Thank you for the code, will try again and update you with the results.
    Raj
    Friday, July 25, 2008 1:08 AM
  • What type of validation are you looking to do against the People Picker?


    Paul Liebrand http://liebrand.wordpress.com http://www.wsswiki.com
    Wednesday, July 30, 2008 4:20 PM
  • I had the exact same problem on a custom application page.

    I solved it with a Custom Field Validator using client side validation

    <script language="javascript">  
        function CheckProjectManager(source, arguments) {  
            if (aspnetForm.ctl00_PlaceHolderMain_ProjectManager_downlevelTextBox.value == "")  
                arguments.IsValid = false;  
            else  
                arguments.IsValid = true;  
        }  
    </script> 

                    <SharePoint:PeopleEditor   
                    ID="ProjectManager"   
                    AllowEmpty="false"   
                    ValidatorEnabled="true"   
                    MultiSelect="false" 
                    runat="server"   
                    SelectionSet="User" 
                    Width="200px"   
                    TabIndex="2" />&nbsp;  
                    <asp:CustomValidator  
                    ID="rfvProjectManager" 
                    runat="server" 
                    ControlToValidate="" 
                    ErrorMessage="Required" 
                    Enabled="true"   
                    ClientValidationFunction="CheckProjectManager">  
                    </asp:CustomValidator> 
     


    Note that the ControlToValidate on the CustomValidator is set to ""

    The ID for the People Editor  ctl00_PlaceHolderMain_ProjectManager_downlevelTextBox was found using IE Developer Toolbar, Note that downlevelTextBox contains the validated userid e.g. DOMAIN\USERID after pressing check user in the peopleeditor.

    /Jan Lange
    • Edited by Jan Lange Thursday, November 13, 2008 8:57 AM
    • Marked as answer by Matt Burnett Friday, December 26, 2008 5:20 PM
    Thursday, November 13, 2008 8:50 AM
  • Hi Jan,

    Thanks a Million. Worked like a wonder.

    I have been trying to get the value from the field itself but could less concentrate on the text box field where the user name is actually stored.
    Raj
    Wednesday, November 19, 2008 2:52 PM