4

Firebug is showing me the following:

firebug screenshot

From the followin Validator initialization:

$("#surveyForm").validate({
    errorPlacement: function(error, element) { 
    if ( element.is(":radio") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' ); 
        } else if ( element.is(":checkbox") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' );
        } else {
        error.appendTo( element.parent() );
        }
},
    rules: {
     ans_23: {
         depends: function(element) {
           return $("#ans_22:checked")
         }
 }
   },
   debug: true
});

The rule is based on the second example under rules here.

The HTML being referenced looks like this

<td class='two_columns'>
<label>
<input type='radio' name='rad_22' id='ans_22' class='required' value='Yes'  />  Other
</label>
<input type='text' name='ans_23' id='ans_23' value='' class='' />
</td>

Anyone know why the depends method would be undefined?

Footnote: I also tried doing this using the rule add method (see below). That form validated and threw no errors when validate() was called and ans_23 did not gain "required" class....

$("#surveyForm").rules("add", {
  ans_23: {
    required: "#ans_22:checked"
  }
});
Sparky
  • 98,165
  • 25
  • 199
  • 285
jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139
  • `return $("#ans_22:checked")` needs to be `return $("#ans_22:checked").length`. – alex Jan 26 '12 at 23:00
  • @alex - thanks. I have added but still am getting "exception occured when checking element ans_23, check the 'depends' method TypeError: $.validator.methods[method] is undefined" from FireBug. – jerrygarciuh Jan 26 '12 at 23:14
  • Make a http://jsfiddle.net and I can help you :) – alex Jan 26 '12 at 23:19
  • 2
    Looks like you might be missing a level... 'depends' should be inside 'required' - see this SO post - http://stackoverflow.com/questions/3975778/jquery-validate-depend-field – ryankeairns Jan 26 '12 at 23:51
  • @alex - thanks! here's two: http://jsfiddle.net/pbSPn/ http://jsfiddle.net/AVG86/2/ – jerrygarciuh Jan 27 '12 at 00:04
  • @mudfalcon - Thanks! That solves it when specifying in rules()! Add here as an answer? – jerrygarciuh Jan 27 '12 at 00:10

1 Answers1

6

I saw a comment reply from you to convert my comment to an answer... so here goes:

$("#surveyForm").validate({
    errorPlacement: function(error, element) { 
    if ( element.is(":radio") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' ); 
        } else if ( element.is(":checkbox") ) {
        error.appendTo ( '#' + element.attr('name') + '_multiError' );
        } else {
        error.appendTo( element.parent() );
        }
},
    rules: {
     ans_23: {
         required: {
           depends: function(element) {
             return $("#ans_22:checked");
           }
         }
 }
   },
   debug: true
});
ryankeairns
  • 788
  • 4
  • 10
  • Yeah, I wanted to be able to upvote you and credit the answer- thanks very much for the help! My day was much improved by having this resolved! – jerrygarciuh Jan 27 '12 at 03:32
  • This worked but I had to change `return $("#ans_22:checked");` to `return $("#ans_22:checked").length;` – gatzkerob Oct 22 '13 at 01:45