开发者

Remove JQuery validationEngine from form

开发者 https://www.devze.com 2022-12-20 17:33 出处:网络
I am using Position Absolute\'s jQuery validationEngine but I need to remove it after attaching it to a form.

I am using Position Absolute's jQuery validationEngine but I need to remove it after attaching it to a form.

I have 2 submit buttons on my form - one for save and one for save as draft.

I attach the validationEngine to the form when Save is clicked. If the validation fails, and the user clicks Save as Draft (by passing the validation), the validation engin开发者_开发百科e is still attached to the form from when they clicked "save".

What I want to do is:

  • allow the user to attempt to save
  • validation fails and error is displayed
  • and allow them to click save as draft without any validation being performed

I tried the unbind function and it appears to work, but it breaks the submit on the button completely. I just want to remove the validationEngine and allow everything else to work as it was.

Thanks


I added a function to the bottom of the validationEngine javascript file:

disable : function(formobj) {
    $(formobj).unbind("submit");
    $(formobj).die("submit");
    $(formobj).bind("submit", function() {
        return true;
    });
    $(formobj).live("submit", function() {
        return true;
    });
}

You pass in the jQuery form element. You could, of course, do this outside the validationEngine javascript file, I just found it easier to lump everything together :-)

The important bit is that you use both unbind AND die.


Create global Validator variable

var globalValidator = null

Perform your validator logoc----

Then reset as per your logic using

function clearValidatorErrors(){
    if(globalValidator != null){
        globalValidator.resetForm(); 
    } 
}


That library doesn't seem to have any support for unbinding at runtime (which is pretty poor design if you ask me).

Here is the code that binds the 'submit'.

$(this).bind("submit", function(caller){   // ON FORM SUBMIT, CONTROL AJAX FUNCTION IF SPECIFIED ON DOCUMENT READY
    $.validationEngine.onSubmitValid = true;
    $.validationEngine.settings = settings;
    if($.validationEngine.submitValidation(this,settings) == false){
        if($.validationEngine.submitForm(this,settings) == true) {return false;}
    }else{
        settings.failure && settings.failure(); 
        return false;
    }       
})

The problem is, if you have more than one submit event handler attached to that form, .unbind('submit') will kill all of the events you had bound.

Assuming that the event was the last to be bound, you might be able to strip only the last event handler attached to submit:

 var events = $("#myForm").data('events');
 alert(events.submit.length); // should tell you how many events are bound..
 events.submit.splice(events.submit.length - 1, 1); // strip the last event
0

精彩评论

暂无评论...
验证码 换一张
取 消