views:

278

answers:

1

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;
        });
    },
+1  A: 

You can use the ignore option of the validation plugin.

$("#yorform").validate({
    ...
    ignore: "input[type=hidden]"
})

This should for example stop the plugin from validating any hidden inputs

Check the documentation for more info

jitter
wow easy solution. somehow I missed that option. appreciate it greatly!
Chris
Actually, this doesn't seem to completely fix the problem. I'm investigating a fix entitled "findByName does not honour ignore settings," found here: http://plugins.jquery.com/node/9234
Chris
Your fix in combination with the fix on the bug report seems to solve the problem. Thanks!
Chris
Interesting find. Thx for the issue tracker url
jitter