{"id":1721,"date":"2016-11-18T15:44:22","date_gmt":"2016-11-18T14:44:22","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1721"},"modified":"2018-09-10T17:39:04","modified_gmt":"2018-09-10T15:39:04","slug":"execute-action-before-saving-a-form-and-after-form-validation-sharepoint","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2016\/11\/18\/execute-action-before-saving-a-form-and-after-form-validation-sharepoint\/","title":{"rendered":"Execute an action before saving a form but after the form validation [Sharepoint]"},"content":{"rendered":"<p>Let&#8217;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, &#8230;). Here is my solution to do it (using jQuery.Deferred):<\/p>\n<pre class=\"brush:javascript\">\r\n\/\/ this function will do our asynchronous check\r\nfunction beforeSaving() {\r\n  var deferred=jQuery.Deferred();\r\n  setTimeout(function() {\r\n    alert(\"Test Complete!\")\r\n    deferred.reject(); \/\/ if the test failed\r\n    \/\/ or deferred.resolve() to valid the test\r\n  }, 2000)\r\n  return deferred;\r\n}\r\n\/\/ force PostBackRequired to true in the context, otherwise it won't work if you open it into a modal\r\nWPQ2FormCtx.PostBackRequired=true;\r\n\/\/ we override SPClientForms.ClientFormManager.SubmitClientForm\r\nSPClientForms.ClientFormManager.SubmitClientForm=function(b){\r\n  var a=SPClientForms.ClientFormManager.GetClientForm(b);\r\n  var res = (a!=null&#038;&a.SubmitClientForm());\r\n  \/\/ if the form is not valid, then res===true\r\n  if (res === true) return true;\r\n  else {\r\n    \/\/ at this stage, all fields' value have been saved into a hidden input\r\n    \/\/ e.g. document.querySelector('input[type=\"hidden\"][id$=\"Field_x0020_Name\"]')\r\n    \/\/ all these saved values will be sent back to the server when \"WebForm_DoPostBackWithOptions\" is executed\r\n\r\n    \/\/ if the form is valid we now want to do our asynchronous check\r\n    beforeSaving().done(function() {\r\n      \/\/ our test is valid too so we can send the form to the server\r\n      WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($get(WPQ2FormCtx.SubmitButtonID).name, \"\", true, \"\", \"\", false, true))\r\n    }).fail(function() {\r\n      \/\/ if it fails we just unblock the save button\r\n      $get(WPQ2FormCtx.SubmitButtonID).disabled=false;\r\n    })\r\n  }\r\n  \/\/ to make sure the form won't be submitted yet\r\n  return true;\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;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, &#8230;). 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() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[170,13,33],"tags":[123,24,152,158,117],"class_list":["post-1721","post","type-post","status-publish","format-standard","hentry","category-english","category-niveau-intermediaire","category-programmation","tag-english","tag-javascript","tag-niveau-intermediaire","tag-programmation","tag-sharepoint"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1721"}],"version-history":[{"count":7,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1721\/revisions"}],"predecessor-version":[{"id":1919,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1721\/revisions\/1919"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}