none
Required Field (Target Audiences) is not stopping submit!! RRS feed

  • Question

  • OK..this is kooky. I am using the Target Audiences field in SharePoint 2007 enterprise. I have set it to required in the column definition (settings page..not code) and the form is allowing it to submit with nothing in it. When I view source I see that there is a parameter called "allowEmpty" which is set to 1 (which I take to mean true as false is generally 0). So How does one require that this field contain a value before letting thep age submit!?!

    Thanks!


    Love them all...regardless. - Buddha

    Wednesday, February 20, 2013 7:54 PM

All replies

  • Hi there,

    It looks like SharePoint by default fills this field with the text "No targeting" when it's left empty which causes it to pass the required validation part. I don't see anyway to make this field mandatory outside of customizing the editform.aspx.

    I would do it using some jquery. Unfortunately I'm not sure how to stop the submit action so using the below solution, you'd have to create a custom edit form. Youd then have to create a custom save button (and hide the default one) with lets say class "CustomButton" and add something like the following jquery to the aspx page:

    <script type="text/javascript" src="_layouts/JQuery/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    $(function() {
    	$('.CustomButton').click(function(event){
    		var AudiencePerson = $('.ms-inputuserfield[title=Audience Editor]');
    		if(AudiencePerson.html() === ''){
    			$(AudiencePerson).append('<p class="ErrorMSG" style="color:red;">You must specify a value for the above required field</p>')						
    		} else {
    			$('[id*=SaveItem]').click();
    		}
    	});	
    });		
    </script>

    Basically this code will fire a custom validation on the  Audience Targeting field and then if passed, will send a click event to the real hidden save button.


    May your PC's be fast and your restarts few.



    • Edited by Sean Heinen Friday, February 22, 2013 9:31 AM
    Friday, February 22, 2013 9:28 AM
  • Hi Sean,

    Thanks for the reply. Unfortunately the Target Audience filed doesn't present itself so straight forward. The id fields that are generated, of which there are 4 in total, appear as:"ctl00_m_g_85773260_0e11_40a7_a37f_7b001812b418_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_hiddenSpanData"

    I have used JQuery in an attempt to write to all of the fields it presents to no avail. Even a custom submit requires a handle to the content but there doesn't appear to be one! I'm thinking this is for security reasons perhaps. I've opened a ticket with Microsoft.

    


    Love them all...regardless. - Buddha

    Friday, February 22, 2013 11:38 AM
  • Hmm. With the power of jquery selectors you should be able to though.

    e.g.

    Below the Target Audience field box html code i get when using web dev tools:

    <DIV onfocusin="this._fFocus=1;saveOldEntities('ctl00_m_g_d8b1b6fa_4d61_49e1_9b3e_5c8d9eb979d9_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_upLevelDiv')" onblur="updateControlValue('ctl00_m_g_d8b1b6fa_4d61_49e1_9b3e_5c8d9eb979d9_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00')" style="OVERFLOW-X: hidden; OVERFLOW-Y: auto; BACKGROUND-COLOR: window; WIDTH: 100%; HEIGHT: 18px; COLOR: windowtext" onkeydown="return onKeyDownRw(this, 'ctl00_m_g_d8b1b6fa_4d61_49e1_9b3e_5c8d9eb979d9_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00', 3, true, event);" ondragstart=canEvt(event); id=ctl00_m_g_d8b1b6fa_4d61_49e1_9b3e_5c8d9eb979d9_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_upLevelDiv oncopy=docopy(); class=ms-inputuserfield title="Audience Editor" onfocusout=this._fFocus=0; tabIndex=0 onkeyup="return onKeyUpRw('ctl00_m_g_d8b1b6fa_4d61_49e1_9b3e_5c8d9eb979d9_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00');" contentEditable=true onpaste=dopaste(); onclick="onClickRw(true, true);" name="upLevelDiv" AutoPostBack="0" onChange="updateControlValue('ctl00_m_g_d8b1b6fa_4d61_49e1_9b3e_5c8d9eb979d9_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00')"></DIV>

    From the above you'll notice the div class is:

    class=ms-inputuserfield

    And the title of the same div is (my reason for checking the title too is there might be other people pickers in the form which all use the same above class.):

    title="Audience Editor"

    The below selector will retrieve all controls with class=ms-inputuserfield and title=Audience Editor 

    $('.ms-inputuserfield[title=Audience Editor]');

    All this being said, it might not be the only way to get what you want, and not the prettiest but it will do the job.

    Sean


    May your PC's be fast and your restarts few.

    EDIT:

    Stupid me just realised there's no reason you'd need to include the class in your selector since the title is pretty unique. So just the below should do:

    $('[title=Audience Editor]');

    • Edited by Sean Heinen Friday, February 22, 2013 12:31 PM
    Friday, February 22, 2013 12:29 PM
  • I agree, JQuery should be able to! The Target Audience is a special class which I believe derives from a Note field.  But getting a reference is no good if I can't then write to the field. Try to write to the field using JQuery, which is what I'm attempting to do. I could very well be missing something and I'd rather not wait around for Microsoft to get back to me. I would be happy if I caould read from the field too just to verify it's empty, but the best way for me to do this is to write to the field. 

    Love them all...regardless. - Buddha

    Friday, February 22, 2013 7:31 PM
  • So Microsoft got back to me and had nothing to say about it. They suggested I handle this a different way. Apaprently the interface is broken and it won't be fixed.

    Love them all...regardless. - Buddha

    Wednesday, March 6, 2013 9:43 PM