Validate People Field in SharePoint Form

I am working adding a validator to a people field in a SharePoint form. The validator seems to be flagging no matter if the requester and approver are the same or not. I thought I had it working but when I changed the fields to not match, the validator still shows up. Any suggestions? My code:

fd.rendered(function() {
fd.field('ManagertoApprove').addValidator({
name: 'Manager to Approve',
error: 'The manager must be different from the requestor.',
validate: function(value) {
if (fd.field('USLRequestor').value == value) {
return true;
}
return false;
}
});
});

Hello @volkjr,

The Person or Group field value is an object, so you need to compare key values. And also, please use spRendered() event in SharePoint forms, rendered() is for Public forms.

Please test this code:

fd.spRendered(function() {
    fd.field('ManagertoApprove').addValidator({
        name: 'Manager to Approve',
        error: 'The manager must be different from the requestor.',
        validate: function(value) {
            if (fd.field('USLRequestor').value && value) {
                return !(fd.field('USLRequestor').value.EntityData.Email == value.EntityData.Email);
            }
        }
    });
});

That worked! Thanks so much! I knew I was missing something tied into the people field but wasn't sure what it was.

1 Like