2013-05-03 20 views
1

我使用的是使用追加到showError功能jQuery驗證和jquery.validate.unobtrusive

  • jquery.validate.min.js
  • jquery.validate.unobtrusive.min第三方工具。 js

當我輸入下面顯示的消息框。工具驗證的自定義驗證失敗。我會假設它也使用「showError」。我很欣賞這是一個很難回答完整代碼庫的問題。我的問題是...

是否有在showErrors之後發生的事件。或者有沒有辦法擴展到這個功能,而不是覆蓋它。

$.validator.setDefaults({ 
    showErrors: function (errorMap, errorList) { 
     $(".messagebox").show(); 
    } 
}); 

回答

2

是否有showErrors後發生的事件。

所有回調函數見文檔:

http://docs.jquery.com/Plugins/Validation/validate#toptions

還是有辦法來擴展這個功能,而不是覆蓋 它。

否。使用自定義回調函數(over-ride)是執行此操作的典型方法。

Looking inside the plugin;它會檢查,如果你有一個自定義的回調函數聲明,然後用你的,而不是默認的...

// http://docs.jquery.com/Plugins/Validation/Validator/showErrors 
    showErrors: function(errors) { 
     if (errors) { 
      // add items to error list and map 
      $.extend(this.errorMap, errors); 
      this.errorList = []; 
      for (var name in errors) { 
       this.errorList.push({ 
        message: errors[name], 
        element: this.findByName(name)[0] 
       }); 
      } 
      // remove items from success list 
      this.successList = $.grep(this.successList, function(element) { 
       return !(element.name in errors); 
      }); 
     } 
     if (this.settings.showErrors) { 
      this.settings.showErrors.call(this, this.errorMap, this.errorList); 
     } else { 
      this.defaultShowErrors(); 
     } 
    } 


    defaultShowErrors: function() { 
     var i, elements; 
     for (i = 0; this.errorList[i]; i++) { 
      var error = this.errorList[i]; 
      if (this.settings.highlight) { 
       this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass); 
      } 
      this.showLabel(error.element, error.message); 
     } 
     if (this.errorList.length) { 
      this.toShow = this.toShow.add(this.containers); 
     } 
     if (this.settings.success) { 
      for (i = 0; this.successList[i]; i++) { 
       this.showLabel(this.successList[i]); 
      } 
     } 
     if (this.settings.unhighlight) { 
      for (i = 0, elements = this.validElements(); elements[i]; i++) { 
       this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass); 
      } 
     } 
     this.toHide = this.toHide.not(this.toShow); 
     this.hideErrors(); 
     this.addWrapper(this.toShow).show(); 
    }