jquery Validation Engine funcCall not working if only rule
Asked Answered
B

2

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?

Blocked answered 24/4, 2013 at 2:30 Comment(0)
M
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;
   }
}
Messing answered 3/5, 2013 at 22:18 Comment(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!Blocked
Hi @Blocked I know this is old thread, it helps me a lot with legacy project, may I know how you handle custom errormsg text?Tsuda
C
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 :

Caudle answered 27/4, 2017 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.