Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery Validation Engine funcCall not working if only rule

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?

like image 681
sunzyflower Avatar asked Apr 24 '13 02:04

sunzyflower


2 Answers

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;
   }
}
like image 61
Ammar Hasan Avatar answered Jan 04 '23 15:01

Ammar Hasan


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 :

  • https://github.com/posabsolute/jQuery-Validation-Engine/issues/392
  • https://github.com/posabsolute/jQuery-Validation-Engine/pull/785
like image 20
TheDeveloo Avatar answered Jan 04 '23 15:01

TheDeveloo