Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bassistance jquery validation plugin - dependency-mismatch

I have searched stack overflow and can't find an answer to my question.

I am using bassistance jquery validation plugin, and it is working well all except for one particular section, custom methods.

The methods are working it is just that calling $('selector').valid() is telling me the element is invalid, even though it should be valid.

This is due to the this.optional(element) returning "dependency-mismatch".

My question is:

What does dependency-mismatch mean when checking for optional on an element?

Does dependency mean that it is optional, but has other dependencies?

So should my custom method check the optional field for "dependency-mismatch"??

I am unclear what it is suppose to return and why.

should my custom method also return dependency-mismatch? or should I deal with it??

This is my custom method:

$.validator.addMethod(
        "ukphonenumber",
        function (value, element, validate) {

            if (validate) {
                var regexp = "\\s*\\(?((\\+0?44)?\\)?[ \\-]?(\\(0\\))|0)((20[7,8]{1}\\)?[ \-]?[1-9]{1}[0-9]{2}[ \\-]?[0-9]{4})|([1-8]{1}[0-9]{3}\\)?[ \\-]?[1-9]{1}[0-9]{2}[ \\-]?[0-9]{3}))\\s*";
                var re = new RegExp(regexp);
                return this.optional(element) || re.test(value);
            }
            else {
                return true;
            }
        },
        "Please check your input."
    );

And this adding the rule to my input:

$("#<%=txtTelephoneNumber.ID %>").rules("add", {
            required: false,
            ukphonenumber: true,
            messages: {
                required: "Please enter your telephone number",
                ukphonenumber: "Your telephone number is invalid"
            }
        });
like image 736
jimplode Avatar asked Jan 20 '11 17:01

jimplode


1 Answers

I also emailed the developer of the library and got this response: Which solved the problem.

is there any reason to use required:false instead of required:true? If the field isn't required, leave out the required rule instead of setting it to false.

You could also try and leave out the call to this.optional(element) within your custom method.

Regards Jörn

like image 194
jimplode Avatar answered Sep 20 '22 23:09

jimplode