Overriding jQuery validation highlight / unhighlight methods

对着背影说爱祢 提交于 2019-12-24 13:15:57

问题


apologies if this has been answered before, I couldn't dig anything up when searching.

I am overriding jQuery validator's highlight & unhighlight methods - but I wish to continue calling the 'stock' version of the methods also. In C# this would be like calling base.unhighlight, for example.

As it stands, I am forced to copy & paste code out of the original thus simulating a call to the parent.

jQuery.validator.setDefaults({
    unhighlight: function (element, errorClass, validClass) {
        // base.unhighlight(element, errorClass, validClass) // <-- desired functionality
    ...

How would I go about doing this? Thanks a lot--


回答1:


With the way this is structured, that's about the best you could do without modifying the plugin. The reason for this, is when you call the setDefault method, it overwrites the plugins own copy of the defaults. This proved hard to get around, but I fixed it by separating the defaults object out & and making a copy of it to refer back to. Honestly copying the simple logic from the plugin source might be the better route at this point, because you should only be calling setDefaults once for your entire implementation.

See my commits here & here. Feel free to fork if you wish.

Basically, now the function you're calling has an extra param, _orig (or whatever you wish). Same goes for highlight.

unhighlight: function (el, error, valid, _orig) {
  _orig(el, error, valid); // runs directly from the defaults.
  // do other stuff.
}

Here's the file

Edit: Wow. That plugin's scope is waayy advanced. It took longer than I thought to implement my solution. Here's a working demo: http://jsfiddle.net/fjMFk/24/




回答2:


Of course the other way to go about this, as you suggested, is to copy the defaults from the source. Turns out, this may be a smarter approach, because as I said in my other answer, you should only have to call setDefaults once.

I'm not sure how familiar you are with jQuery/JS in general. But normally, you pass in options with each call you make to the validator.

So if I have three forms, I can do:

$('#form1').validate({
   unhighlight: function(el, error, valid){ el.append('<div class="errormsg">Required for Form1</div>'); }
});

$('#form2').validate({
   unhighlight: function(el, error, valid){ $(el).append('<div class="errormsg">Required for Form2</div>'); }
});

$('#form3').validate({
   unhighlight: function(el, error, valid){ $(el).append('<div class="errormsg">Required for Form3</div>'); }
});

But if I want to change the highlight method globally, I can do:

jQuery.validator.setDefaults({
   highlight: function(el, error, valid){ $(el).siblings('.errormsg').remove; }
});

So really you should only need to call the setDefaults method once. And surely copy pasting once wouldn't be too hard.



来源:https://stackoverflow.com/questions/11477848/overriding-jquery-validation-highlight-unhighlight-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!