How to use Twitter Bootstrap popovers for jQuery validation notifications?

后端 未结 16 1701
无人共我
无人共我 2020-11-30 16:49

I can make popovers appear using bootstrap easily enough, and I can also do validations using the standard jQuery validation plugin or the jQuery validation engine, but I ca

16条回答
  •  囚心锁ツ
    2020-11-30 17:19

    This is how I made it happen. But it involves making 2 changes to the validate script (I got the code for bootstrap 1.4 here and then modified it - http://mihirchitnis.net/2012/01/customizing-error-messages-using-jquery-validate-plugin-for-twitter-bootstrap/)

    My call to validate:

        $("#loginForm").validate({
      errorClass: "control-group error",
      validClass: "control-group success",
      errorElement: "span", // class='help-inline'
      highlight: function(element, errorClass, validClass) {
        if (element.type === 'radio') {
            this.findByName(element.name).parent("div").parent("div").removeClass(validClass).addClass(errorClass);
        } else {
            $(element).parent("div").parent("div").removeClass(validClass).addClass(errorClass);
        }
      },
      unhighlight: function(element, errorClass, validClass) {
        if (element.type === 'radio') {
            this.findByName(element.name).parent("div").parent("div").removeClass(errorClass).addClass(validClass);
        } else {
            $(element).parent("div").parent("div").removeClass(errorClass).addClass(validClass);
        }
      }
    });
    

    Then you need to change 2 things in jquery.validate.js
    1. apply this fix - https://github.com/bsrykt/jquery-validation/commit/6c3f53ee00d8862bd4ee89bb627de5a53a7ed20a
    2. After line 647 (in the showLabel function, create label part) after line .addClass(this.settings.errorClass) add line: .addClass("help-inline")
    Someone can maybe find a way to apply the second fix in the validate function, but I havent found a way, since showLabel is called after highlight.

提交回复
热议问题