Let’s say you want to do an asynchronous request just before saving a Sharepoint form, but after Sharepoint verified all the fields in your form (for mandatory ones, or for format, …). Here is my solution to do it (using jQuery.Deferred):
// this function will do our asynchronous check function beforeSaving() { var deferred=jQuery.Deferred(); setTimeout(function() { alert("Test Complete!") deferred.reject(); // if the test failed // or deferred.resolve() to valid the test }, 2000) return deferred; } // force PostBackRequired to true in the context, otherwise it won't work if you open it into a modal WPQ2FormCtx.PostBackRequired=true; // we override SPClientForms.ClientFormManager.SubmitClientForm SPClientForms.ClientFormManager.SubmitClientForm=function(b){ var a=SPClientForms.ClientFormManager.GetClientForm(b); var res = (a!=null&&a.SubmitClientForm()); // if the form is not valid, then res===true if (res === true) return true; else { // at this stage, all fields' value have been saved into a hidden input // e.g. document.querySelector('input[type="hidden"][id$="Field_x0020_Name"]') // all these saved values will be sent back to the server when "WebForm_DoPostBackWithOptions" is executed // if the form is valid we now want to do our asynchronous check beforeSaving().done(function() { // our test is valid too so we can send the form to the server WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($get(WPQ2FormCtx.SubmitButtonID).name, "", true, "", "", false, true)) }).fail(function() { // if it fails we just unblock the save button $get(WPQ2FormCtx.SubmitButtonID).disabled=false; }) } // to make sure the form won't be submitted yet return true; };
for info, it doesnt res variable return false and save the form even though DATE and Person field are invalid, so if you have any input created by you, it will loose the value,
I am using its function, but the Deferred object fails.
How can I make it so that it does not fail?
liriarte > it’s impossible to help you in the comments of this blog. Please ask your question to https://stackoverflow.com/ giving code and context.