Skip to content

Instantly share code, notes, and snippets.

@pniaps
Created November 25, 2016 13:51
Show Gist options
  • Save pniaps/b4e39f5a35786f67ffb87de08e5f84a2 to your computer and use it in GitHub Desktop.
Save pniaps/b4e39f5a35786f67ffb87de08e5f84a2 to your computer and use it in GitHub Desktop.
Jquery Validate with suppor to array of fields
if ($.validator) {
//returns all elements, even with the same name (default returns only the first element for each name)
$.validator.prototype.elements = function() {
var validator = this;
// select all valid inputs inside the form (no submit or reset buttons)
return $( this.currentForm )
.find( "input, select, textarea" )
.not( ":submit, :reset, :image, [disabled], [readonly]" )
.not( this.settings.ignore )
.filter( function() {
if ( !this.name && validator.settings.debug && window.console ) {
console.error( "%o has no name assigned", this );
}
// select only elements with rules specified
return validator.objectLength( $( this ).rules() );
});
};
$.validator.prototype.errorsFor = function( element ) {
var name = this.idOrName( element ),
describer = $( element ).attr( "aria-describedby" ),
selector = "label[for='" + name + "'], label[for='" + name + "'] *";
// aria-describedby should directly reference the error element
if ( describer ) {
selector = selector + ", #" + describer.replace( /\s+/g, ", #" );
}
if(this.idOrName( element ).indexOf('[]') > -1){
//if element is array, label is next to element
selector = $(element).next();
}
return this
.errors()
.filter( selector );
};
$.validator.prototype.findLastActive = function () {
var lastActive = this.lastActive;
return lastActive && $.grep(this.errorList, function (n) {
return n.element === lastActive;
}).length === 1 && lastActive;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment