Hi all,
By default, Zend_Form creates a hidden input field for each checkbox input in the form. These inputs share the same name.
<input type="hidden" name="zend-cb" value="">
<input type="checkbox" name="zend-cb" id="zend-cb" value="1">
I want to require the checkbox, so I set up the following rule in my jquery plugin validator (http://bassistance.de/jquery-plugins/):
'zend-cb': {
required: true
}
Unfortunately, the Jquery Validation plugin always validates the hidden field instead of the checkbox. Is there a way I can have it validate the checkbox instead? I realize I could change my Zend Decorator to omit the hidden field, but I'd like to find a pure javascript solution.
Solution
Two steps are needed to get around this problem.
1) Add ignore: "input[type=hidden]" as an option to the validate method.
$('#myForm').validate( {
ignore: "input[type=hidden]",
rules: { ... }
}
2) Open jquery.validate.js and update the findByName method to use the ignore filter. Bug report filed by adamnfish on the jquery plugin site.
findByName does not honour ignore settings
findByName: function( name ) {
// select by name and filter by form for performance over form.find("[name=...]")
var form = this.currentForm;
return $(document.getElementsByName(name)).not(this.settings.ignore).map(function(index, element) {
return element.form == form && element.name == name && element || null;
});
},