How to use Twitter Bootstrap popovers for jQuery validation notifications?

后端 未结 16 1601
无人共我
无人共我 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: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
            });
            _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.

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

    This is how I did it with Bootstrap 2.x and jQuery Validate 1.9

    $('#form-register').validate({ errorElement: 'span', errorClass:'help-inline', highlight:    function (element, errorClass) {
            $(element).parent().parent().addClass('error');
        }, unhighlight: function (element, errorClass) {
            $(element).parent().parent().removeClass('error');
        }});
    
    0 讨论(0)
  • 2020-11-30 17:28

    This is a hands-on example:

    $('form').validate({
        errorClass:'error',
        validClass:'success',
        errorElement:'span',
        highlight: function (element, errorClass, validClass) { 
            $(element).parents("div[class='clearfix']").addClass(errorClass).removeClass(validClass); 
        }, 
        unhighlight: function (element, errorClass, validClass) { 
            $(element).parents(".error").removeClass(errorClass).addClass(validClass); 
        }
    });
    

    enter image description here

    It doesn't really use bootstrap popovers, but it looks really nice and is easy to achieve.

    UPDATE

    So, to have popover validation you can use this code:

    $("form").validate({
      rules : {
        test : {
          minlength: 3 ,
          required: true
        }
      },
      showErrors: function(errorMap, errorList) {
        $.each(this.successList, function(index, value) {
          return $(value).popover("hide");
        });
        return $.each(errorList, function(index, value) {
          var _popover;
          _popover = $(value.element).popover({
            trigger: "manual",
            placement: "top",
            content: value.message,
            template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
          });
          // Bootstrap 3.x :      
          //_popover.data("bs.popover").options.content = value.message;
          // Bootstrap 2.x :
          _popover.data("popover").options.content = value.message;
          return $(value.element).popover("show");
        });
      }
    });
    

    You get something like this:

    enter image description here

    Check out the jsFiddle.

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

    This jQuery extension for jQuery Validation Plugin (tested with version 1.9.0) will do the trick.

    https://github.com/tonycoco/rails_template/blob/master/files/assets/javascripts/jquery.validate.bootstrap.js

    This also adds in some Rails-esk error messaging.

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

    I prefer to change the CSS of bootstrap. Just added the classes of jQuery validate in the right place. field-validation-error and input-validation-error

        form .clearfix.error > label, form .clearfix.error .help-block, form .clearfix.error .help-inline, .field-validation-error {
      color: #b94a48;
    }
    form .clearfix.error input, form .clearfix.error textarea, .input-validation-error {
      color: #b94a48;
      border-color: #ee5f5b;
    }
    form .clearfix.error input:focus, form .clearfix.error textarea:focus, .input-validation-error:focus {
      border-color: #e9322d;
      -webkit-box-shadow: 0 0 6px #f8b9b7;
      -moz-box-shadow: 0 0 6px #f8b9b7;
      box-shadow: 0 0 6px #f8b9b7;
    }
    
    0 讨论(0)
  • 2020-11-30 17:30

    Many thanks for the heads up! Here is my version for Bootstrap but with Tooltips. In my opinion it's more elegant than popovers. I know the question was for popovers so please do not vote down for this reason. Maybe somebody will like it this way. I love when I'm searching for something and I found new ideas on Stackoverflow. Note: no markup on form is necessary.

        $('#LoginForm').validate({
            rules: {
                password: {
                    required: true,
                    minlength: 6
                },
    
                email_address: {
                    required: true,
                    email: true
                }
            },
            messages: {
                password: {
                    required: "Password is required",
                    minlength: "Minimum length is 6 characters"
                },
                email_address: {
                    required: "Email address is required",
                    email: "Email address is not valid"
                }
            },  
            submitHandler: function(form) {
                form.submit();
            },
    
            showErrors: function (errorMap, errorList) {
    
                $.each(this.successList, function (index, value) {
                    $('#'+value.id+'').tooltip('destroy');
                });
    
    
                $.each(errorList, function (index, value) {
    
                    $('#'+value.element.id+'').attr('title',value.message).tooltip({
                        placement: 'bottom',
                        trigger: 'manual',
                        delay: { show: 500, hide: 5000 }
                    }).tooltip('show');
    
                });
    
            }
    
        }); 
    
    0 讨论(0)
提交回复
热议问题