6

I want to validate custom modal dialog form and if its not ok, prevent form from closing. For this purpose, I attached to X-close-dialog-button. Problem is to prevent it's default behavior.

function fValidateBeforeClose(event) {
    event.stopImmediatePropagation(); // Not working
}    
function fAddOnCloseHandler() {
    var oCloseButton = jQuery("a[id^='DlgClose']", parent.document);
    oCloseButton.on( "click", function (event) { fValidateBeforeClose(event)} ); 
}       
_spBodyOnLoadFunctionNames.push( ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js") );
4

Good way to remove handlers from dialog's close button is using $clearHandlers Method.

Check code below:

function fValidateBeforeClose(event) {
    var isValid = true;
    //set IsValid value
    if(isValid)
    {
       //close dialog
       SP.UI.ModalDialog.get_childDialog().close();
    }
    else
    {
      alert('Not valid');
    }
}  

function fAddOnCloseHandler() {
    var oCloseButton = $("a[id^='DlgClose']");

    $clearHandlers(oCloseButton.get(0));

    $(oCloseButton).on("click", function (event) { fValidateBeforeClose(event)} );
}   

_spBodyOnLoadFunctionNames.push( ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js") );
  • Worked. I modify your answer, to remove unnecessary element search in DOM. – dbardakov May 22 '13 at 11:49
  • Ok, I approved it. – Andrew May 22 '13 at 12:01
3

In SharePoint 2013, this works fine after I added "window.parent.document" in the following line:

var oCloseButton = $("a[id^='DlgClose']",window.parent.document);
1

I think you should remove old event handler

 var js = oCloseButton.attr("onclick");
 oCloseButton.attr("onclick", "");//remove sharepoint click handler...
  • Good catch,jQuery(oCloseButton).off().find("*").off(); seems working. Though, I liked "the SharePoint way" provided by @Andrew better. – dbardakov May 22 '13 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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