6

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?

sunzyflower
  • 195
  • 3
  • 13

2 Answers2

13

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;
   }
}
Ammar Hasan
  • 146
  • 1
  • 2
  • Thank you so much! It actually started giving me double message with this so I took out the return and left just the rules.push('required') and that worked perfectly! – sunzyflower May 05 '13 at 00:14
  • Hi @sunzyflower I know this is old thread, it helps me a lot with legacy project, may I know how you handle custom errormsg text? – Yosep G Mar 10 '22 at 11:25
1

Use

funcCallRequired[validatePassportRequired]

instead of

funcCall[validatePassportRequired]

This will add required internally without having a double message.

If you want more information about the (old) issue :

TheDeveloo
  • 413
  • 4
  • 13