28

I am performing some custom validation on a custom list form using javascript. I had my code in "PreSaveAction()" and it was working. At some point it stopped working and I determined that the code was now calling "PreSaveItem()" instead.

My fix is to simply rename my function, but since I don't know why exactly it started calling PreSaveItem I'd like to know what the difference is.

3 Answers 3

24

In SharePoint PreSaveItem function is called when onClick event handler is fired for Save button in List Forms.

PreSaveItem function:

function PreSaveItem()
{   
   if ("function"==typeof(PreSaveAction))
   {
      return PreSaveAction();
   }
   return true;
}

PreSaveAction function from another hand, allows to override behavior for a Save button. It is commonly used for client side validation, for example:

function PreSaveAction() {   
   return ifValidSSN();
}
1
  • 8
    To add to your answer, PreSaveItem is defined in form.js and automatically included. PreSaveAction only exists if it is defined by the user.
    – Chloraphil
    Commented Mar 28, 2014 at 14:41
7

I just want to add one thing, you should not override PreSaveItem - this is a SharePoint built in function. Instead, like mentioned above - override/create PreSaveAction.

But: be sure to remember you may not be the only one doing so.

So, the correct way to do it is this, which will call other overrides if existed before your code:

__OriginalPreSaveAction = null;
function MyPreSaveAction (){
var amIValid = true;//do your thing
var areOtherValid = typeof(__OriginalPreSaveAction) === "function" ? __OriginalPreSaveAction():true;

return amIValid && areOtherValid;
}

function RegisterMyPreSaveAction(){
__OriginalPreSaveAction = PreSaveAction;
PreSaveAction = MyPreSaveAction;
}

and to register, simply call

RegisterMyPreSaveAction();
3

Here is very good blog article, about the difference of PreSaveItem and PreSaveAction.

Quote:

The function "PreSaveItem" is simply checking if a function with name "PreSaveAction" exists or not and if it exists invoke it and return its returned value (true/false) or return "true" by default if no functions exists with this name.

PreSaveItem call is made in the onclick handler or the button and is as follows:

if (!PreSaveItem())
          return false;
if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ1'))
          return false;

The PreSaveItem function is declared is like this:

function PreSaveItem(){
    a:;
    return "function" == typeof PreSaveAction?PreSaveAction():true
}

It seems that PreSaveAction is much better and cleaner approach to make some actions before submit. But you have to check and make sure if SharePoint default PreSaveItem function is loaded correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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