How to use Twitter Bootstrap popovers for jQuery validation notifications?

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

    tl;dr avoid needing to enumerate explicit popovers by using a hash map to store the ids of the elements, and creating popovers on-the-fly (mashup Jeffrey Gilbert and Kenny Meyer's approaches).

    Here's my take, which fixes the flickering problem mentioned by others, but unlike @Jeffrey Gilbert's answer, does not use a list (errorStates) but rather uses an error map. Hash maps FTW. I think I remember reading somewhere that every problem in CS can be solved with a hash map :)

    var err_map = new Object();     // <--- n.b.
    $("form#set_draws").validate({
      rules: {
        myinput: { required: true, number: true },
      },
      showErrors: function(errorMap, errorList) {
        $.each(this.successList, function(index, value) {
          if (value.id in err_map)
          {
            var k = err_map[value.id];
            delete err_map[value.id]; // so validation can transition between valid/invalid states
            k.popover("hide");
          }
        });
        return $.each(errorList, function(index, value) {
          var element = $(value.element);
          if( ! (value.element.id in err_map) ) {
            var _popover = element.popover({
              trigger: "manual",
                     placement: "top",
                     content: value.message,
                     template: "

    " }); _popover.data("popover").options.content = value.message; err_map[value.element.id] = _popover; return err_map[value.element.id].popover("show"); } }); } });

    Thanks to all others who posted ideas on this.

提交回复
热议问题