Trouble Attaching Call Back to Unobtrusive Validation Show Error

穿精又带淫゛_ 提交于 2019-12-11 00:04:28

问题


I'm having trouble attaching a call back to jquery unobtrusive validate. What I want to do is hide an informational message any time a field validation error is shown. They both take up the same area on the page. I can't figure out how to attach a callback to the show error method. I have one that works if you call validate but the error messages are shown before even change for the field is fired.

Here is a jsfiddle demoing my problem. http://jsfiddle.net/K6NcF/4/

This is the callback that only runs when validate() is called.

$('#theForm').bind('invalid-form.validate',function(){
    $('.info-box').hide();
});

Any help would be appreciated.


回答1:


You can access the highlight and unhighlight callbacks when using unobtrusive like this

// grab a reference to the validator instance
var v = $('form').validate();

// reference the settings property, and the highlight function
var originalHighlight = v.settings.highlight;

You could just overwrite this with your stuff, or the example below shows a way to add your own functionality without overwriting what is there.

var v = $('#theForm').validate();

var originalHighlight = v.settings.highlight;
var originalUnHighlight = v.settings.unhighlight;

v.settings.highlight = function(element, errorClass, validClass)
{
  console.log('doing my stuff here');
  $('.info-box').hide();
  originalHighlight.call(v, element, errorClass, validClass);
}

v.settings.unhighlight = function(element, errorClass, validClass)
{
  console.log('undoing my stuff here');
  $('.info-box').show();
  originalUnHighlight.call(v, element, errorClass, validClass);
}

fiddle here http://jsfiddle.net/K6NcF/8/



来源:https://stackoverflow.com/questions/16196633/trouble-attaching-call-back-to-unobtrusive-validation-show-error

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