1

Environment: SharePoint 2013 foundation

I'd like to make/validate the People Picker field required conditionally in the edit form of a task list. If the task status is changed to Completed, I want the people picker field to be required.

I tried using PreSaveAction to make this happen, but my code is not working properly. It continues to give an alert even when the people picker field is populated. My people picker field is named Assigned To.

What am I missing in this code? I've searched endlessly, but cannot make this happen.

<script type="text/javascript" src="https://mysiteurl/Scripts/jquery-2.1.4.min.js"></script>
<script type="text/javascript" language="javascript"> 
function PreSaveAction() { 
drop = $("select[title='Task Status']").val();
assigned = $().SPServices.SPFindPeoplePicker({
 peoplePickerDisplayName: "Assigned To",
 checkNames: true
 });
    if (drop ==="Completed" && assigned===""){
        alert("Please assign this item to yourself.");
        return false;
    }    
        return true;
}
0

I have done something similar to this and used the PreSaveAction function as well.

This should be a catch-all on a custom new form for all fields. This doesn't contain the conditional logic, but should lay some groundwork on how to select.

The way I was flagging each field was by using the class requiredField on the <tr>, which is done by placing in the custom form or via jQuery based on whatever your conditions are.

function PreSaveAction() {

    // Setting up variables.
    var missingField = false,
        alertString = "You need values for the following:\n";

    // Find all corresponding form values for the class name assigned above.
    $(".requiredField select, .requiredField input[id$='TextField'], .requiredField textarea, .requiredField input[id$='DateTimeFieldDate']").each(function() {
        var field = $(this).closest("td[class='ms-formbody']").siblings("td").find("nobr").text();
        if($(this).val() === "" || $(this).val() === "0") {
            console.log("missing value! - " + field + " - " + this);
            missingField = true;
            alertString += "\n\u2022" + field;
        }
        else {
            console.log("all good! - " + field + " - " + this + " - " + $(this).val());
        }
    });

    // Finding all required multi-select checkboxes and checking if they have at least one value checked.
    $(".requiredField > td > span > table").each(function() {
        var field = $(this).closest("td[class='ms-formbody']").siblings("td").find("nobr").text();
        if($(this).find("input[type='checkbox']").length > 0) {
            if(!$(this).find("input[type='checkbox']").is(":checked")) {
                console.log("missing value! - " + field + " - " + this);
                missingField = true;
                alertString += "\n\u2022" + field;
            }
            else {
                console.log("all good! - " + field + " - " + this);                 
            }
        }
    });

    console.log("-------------------");

    // If there is at least one field missing return to user without saving, otherwise save the item.
    if(missingField) {
        alert(alertString);
        return false;
    }
    else {
        return true;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.