2

So I just found that you can add any custom validation logic. If it's simple enough it's not a problem, right?

$.validator.addMethod('customRule',function(val, el){
    return val > 2;
},'value cannot be smaller than 2');

$('#myInput').rules('add', { customRule : true });

now, even if you have something a little bit more difficult (a REST call for example) it still can be done:

$.validator.addMethod('customRule',function(val, el){
    var validator = this;
    $.get('checkValueOnServer',val).done(function(isValid){
        if(!isValid){
           var errors = {}; 
           errors[el.name] = 'server says: value does not look right!';
           validator.showErrors(errors);
        }
        validator.stopRequest(el, isValid);
    })
    return 'pending';
},'');

now, in my case I need to call a function with a callback, something like that

$.validator.addMethod('customRule',function(val, el){
    var validator = this;
    checkValueOnServer(el, function(isValid){ // so function runs for a few secs and returns a callback with a result 
        if(!isValid){
           var errors = {}; 
           errors[el.name] = 'called function says: value does not look right!';
           validator.showErrors(errors);
        }
        validator.stopRequest(el, isValid);
    });
    return 'pending';
},'');

You see just a good old callback, not deferred object. So in the later case if I call

$('form').valid() it reports it's true, although custom called function reports that the validation rule didn't pass and the element is highlighted and error message is shown. So what's wrong? What's the right way to tell that thing: the form is invalid?

2
  • Please construct a jsFiddle demo of this issue. I almost cannot believe that .valid() is returning true while there are invalid fields - that's not supposed to happen. Clearly, either something is wrong with your code or the plugin... probably not the plugin. Commented Apr 18, 2013 at 20:01
  • I guess I can try workaround by storing last time check timestamp and value. Here's what I'm talking about (only value yet without the timestamp) plnkr.co/edit/eK6OGZ4qto4wqOsBoXBH. I guess I have to decide now what would be minimal timespan within which the value will be assumed correct. Without needing to check again... Commented Apr 18, 2013 at 21:13

1 Answer 1

3

When using a custom validation method, the function typically only returns a true or a false.

  • If your custom validation function returns true, the element passed validation and there is no error message triggered. http://jsfiddle.net/kCqy5/1/

  • If your custom validation function returns false, the element failed validation and an error message is displayed. http://jsfiddle.net/kCqy5/2/

  • If your custom validation function does not return true or false, then the results do not seem to be very predictable. http://jsfiddle.net/kCqy5/

Yours is only returning a string that contains the text 'pending', the required error message is displayed even though the form passes validation. Your usage is outside the scope of the documentation...


As per documentation:

method Callback
The actual method implementation, returning true if an element is valid. First argument: Current value. Second argument: Validated element. Third argument: Parameters.

Sign up to request clarification or add additional context in comments.

8 Comments

@Agzam, I'm not sure what you're showing me because it proves my point. Examining the code in that post, you can see that the variable called nameUnique will contain true when it needs to pass validation.
again, for my case just a simple function that returns false or true not sufficient, I need something that returns a callback and depending on that callback sets the properties to be valid or not
@Agzam, and again, this method only understands true or not true with regards to a custom rule created with addMethod. So what I'm telling you is that returning the string containing 'pending' is no different than returning false... this custom rule will never be satisfied.
@Agzam, you're going to have to explain yourself better than that. My answer is quoting directly from the documentation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.