I have a custom form for an Issues List in SharePoint 2013 and I'm trying to validate two people picker fields upon saving. For one field, if the user is in a SharePoint group, they are required to enter a name in the Assigned To field. I already have code in the document.ready portion of the code to validate they are in the group and a global variable which changes to true and appears to be working. The goal is to check that this variable is true and the people picker is not empty, otherwise, enforce validation. For the second people picker field, if the issue status is "Escalated", then the person assigned the issue must enter a name in the Escalated To field.
With the code I have, I can get both of them to work, but only one at a time. If I try to combine them using else, it doesn't work. This code uses the out of the box error message, which I prefer over an alert. I'm fairly new to jquery and javascript so please excuse my code. It is a patchwork of code from various forums. I wasn't sure if I could reuse the clientPeoplePicker object. I also saw on a forum about returning the "inner" function but I couldn't figure out how to get that to work either. When I have more time, I'm going to start with the basics so I have a good foundation! :) Any help is appreciated. Thanks!
//PreSave function
function PreSaveAction(){
var peoplePickerName = "Escalated To";
clientPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title="' + peoplePickerName + '"]')[0].id];
if (clientPeoplePicker.IsEmpty() && $("input[type=radio][id^='Status']:checked").val()=="Escalated") {
clientPeoplePicker.AllowEmpty = false
clientPeoplePicker.Validate()
$("input.sp-peoplepicker-editorInput[id^='"+ peoplePickerName +"']").focus();
return false;
} else {
return true;
}
var spPPName = "Assigned To"
clientPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title="' + spPPName + '"]')[0].id];
if (clientPeoplePicker.IsEmpty() && envOwner = true) {
clientPeoplePicker.AllowEmpty = false
clientPeoplePicker.Validate()
$("input.sp-peoplepicker-editorInput[id^='"+ spPPName +"']").focus();
return false;
} else {
return true;
}
}