jquery Validation Engine funcCall not working if only rule

sunzyflower picture sunzyflower · Apr 24, 2013 · Viewed 8.2k times · Source

I have an input field that I am trying to add custom validation to (required depending on another field). If I put required AND funcCall() I can see that two errors are returned. If I only put the funcCall nothing is returned. I know it's getting in the function and the condition because I did a console.log() but for some reason it seems like it needs an initial rule to fail to show the error.

Call:

<input type="text" class="validate[funcCall[validatePassportRequired]]" id="form_register_passport_number" value="" name="passport_number" size="50">

Function:

function validatePassportRequired(field, rules, i, options) {
  if ($('#register_for').val()!='Local') {
    return options.allrules.required.alertText;
  }
}

So If I change the Call to:

class="validate[required, funcCall[validatePassportRequired]]"  

I get two * This field is required

Do I have to have another validation rule along with the funcCall?

Answer

Ammar Hasan picture Ammar Hasan · May 4, 2013

just add the following line before returning the error message and instead of required in returning message put the function name before .alertText.

rules.push('required');

@sunzyflower in your case your function would see like this..

function validatePassportRequired(field, rules, i, options) {
   if ($('#register_for').val()!='Local') {
   rules.push('required');  
   return options.allrules.validatePassportRequired.alertText;
   }
}