jQuery Validation Plugin: Invoke errorPlacement function when onfocusout, keyup and click

前端 未结 2 640
[愿得一人]
[愿得一人] 2020-12-17 18:39

I am using the jquery validation plugin and want to use the errorPlacement function to add error messages to the fields title attribute and display just a ✘ next to the fiel

2条回答
  •  遥遥无期
    2020-12-17 19:07

    Thanks jitter,

    I done some digging around and found the same problem.

    I managed to get it working by "hacking" the showLabel function in the jquery.validation.js. It's not pretty but works.

    Overriding the showErrors function option would prevent me from having to change the plugin code so I will take a look.

    Here is the code I used for the showLabel method:

         showLabel: function(element, message) {
    
                // look for existing error message
                var label = this.errorsFor( element );
                // existing error exist?
                if (label.length) {
                    // refresh error/success class
                    label.removeClass().addClass( this.settings.errorClass );
    
                    // check if we have a generated label, replace the message then
                    label.attr("generated");
    
                    // is message empty?
                    if(!message)
                    {
                        // add tick glyph
                        label.html("✔");
    
                        // wipe element title
                        $(element).attr('title', message)
                    }
                    else
                    {
                        // clear error html and add cross glyph
                        label.html("✘");
    
                        // update element title
                        $(element).attr('title', message)
                    }
    
                    // && label.html(message);
                } 
                else {
                    // create label
                    label = $("<" + this.settings.errorElement + "/>")
                        .attr({"for":  this.idOrName(element), generated: true})
                        .addClass(this.settings.errorClass)
                        .html(message || "");
                    if ( this.settings.wrapper ) {
                        // make sure the element is visible, even in IE
                        // actually showing the wrapped element is handled elsewhere
                        label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
                    }
                    if ( !this.labelContainer.append(label).length )
                        this.settings.errorPlacement
                            ? this.settings.errorPlacement(label, $(element) )
                            : label.insertAfter(element);
                }
                if ( !message && this.settings.success ) {
                    label.text("");
                    typeof this.settings.success == "string"
                        ? label.addClass( this.settings.success )
                        : this.settings.success( label );
                }
                this.toShow = this.toShow.add(label);
            }
    

提交回复
热议问题