I managed to validate multiple form fields with identical names, such as first_name[]
, using jQuery Validate plugin and this workaround (quoted here).
However error messages are displayed only for the first instance of a field, not the next ones.
Why is that?
For the record, the solution presented in the links above consists in editing jquery.validate.js and change the checkForm function content into:
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
}
Then using this kind of parameters when calling the plugin:
rules: {
"field_name[]": "required"
}