Reputation: 31250
jQuery plugin's Validation's method Valid()
throws an error this[0] is undefined
when called on empty collection. I thought jQuery convention is not to do anything on empty collection, certainly not throw any error or may be I am missing something. Could someone shed light on it please?
Code fragment from the plugin. this[0] - shown below - throws the error as the collection is empty.
// http://docs.jquery.com/Plugins/Validation/valid
valid: function() {
if ( $(this[0]).is('form')) {
return this.validate().form();
} else {
var valid = true;
var validator = $(this[0].form).validate(); // This is the line that throws error
this.each(function() {
valid &= validator.element(this);
});
return valid;
}
Upvotes: 1
Views: 1108
Reputation: 17640
what I do is just add class to inputs that i want to have validate then my form has an id and i call validate to it
$("#myform").validate()
here is one example of how i use form validation
i also recommend looking at the demos
generally speaking of standard jquery if you apply say like a click event to an element that doesn't exist you don't get errors jquery skips over it. that said maybe validate should do the same but it apparently does not so I would suggest doing it the way I posted or check for the length of the selector before applying validate
Upvotes: 1