Remove JQuery validationEngine from form

2020-07-22 18:44发布

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 engine 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

3条回答
Bombasti
2楼-- · 2020-07-22 18:54

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(); 
    } 
}
查看更多
萌系小妹纸
3楼-- · 2020-07-22 19:05

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
查看更多
乱世女痞
4楼-- · 2020-07-22 19:07

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.

查看更多
登录 后发表回答