How to use Twitter Bootstrap popovers for jQuery validation notifications?

后端 未结 16 1600
无人共我
无人共我 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:32

    Not sure if this is relevant to the discussion because the original poster asked for hooks to show/hide bootstrap popovers.

    I was looking for simple validation and popovers didn't matter. A related post and the first in google search results has already been marked duplicate of this question. So it made sense to mention this excellent @ReactiveRaven's jqValidation JS, aptly called jqBootstrapValidation, that weds well with Twitter Bootstrap. Setup takes a few minutes only. Download here.

    Hope this adds value.

    0 讨论(0)
  • 2020-11-30 17:33

    Here is an update to Kenny Meyer's excellent answer above. There were a couple of issues that prevented it from working for me, which I have addressed in this snippet:

    showErrors: function (errorMap, errorList) {
            $.each(this.successList, function (index, element) {
                return $(element).popover("destroy");
            });
    
            $.each(errorList, function (index, error) {
                var ele = $(error.element); //Instead of referencing the popover directly, I use the element that is the target for the popover
    
                ele.popover({
                        trigger: "manual",
                        placement: "top",
                        content: function(){ //use a function to assign the error message to content
                            return error.message
                        },
                        template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content"><p></p></div></div></div>'
                });
    
                //bs.popover must be used, not just popover
                ele.data("bs.popover").options.content = error.message;
    
                return $(error.element).popover("show");
            });
        }
    
    0 讨论(0)
  • 2020-11-30 17:36

    Checkout this: https://github.com/mingliangfeng/jquery.validate.bootstrap.popover

    It shows how to use Bootstrap popover css, instead of JS. JS method popup will cause blinking issue.

    0 讨论(0)
  • 2020-11-30 17:37

    If using the above Kenny Meyer code for popups, beware that rules that check a field's content but isn't required such as a valid URL will cause the popup to not disappear upon clearing the field. See below onkeyup for solution. If anyone has a better solution, please post.

    onkeyup: function(element, event) {
                if($(element).valid())  {
                    return $(element).popover("hide");
                }
            }
    
    0 讨论(0)
提交回复
热议问题