I need to validate a form that uses hidden inputs for a set of values that get created dynamically. These hidden inputs don’t exist on an empty form but I want to ensure that at least one is created before the form can be submitted.
The validator plugin requires an existing element to attach a rule to it. Is there anyway way to attach a rule to a selector or something similar that would test if an element exists?
The only other option I can think of is to use the submit handler to perform secondary validation.
I’m binding the rules like this:
var elements = $("#" + formId + ' [name=\"' + name + '\"]');
if(elements.length > 0){
elements.each(function(i){
$(this).rules("add", {
required: true
});
});
}
So there’s no real way to add a rule to an element that doesn’t exist yet. I can (and do) add a rule when the element is created dynamically, but how do I test if that element hasn’t been created?
Any suggestions?
In what way do you attach these rules exactly? Without knowing what’s going on, it’s hard to answer your question.
Maybe you can just attach a validator rule to the body element, or to the form element and check the following?
Also, I would imagine that you can create a stub div element and attach to that.