Alex Rivera | Logout

jquery validator addmethod custom message

Asked 2009-06-10T15:32:19.220
19

I've created a method for jquery's validator plugin, that works like the remote rule. The difference is that I'd like to display a dynamic error message (based on the ajax response).

jQuery.validator.addMethod("duplicate", function(value, element, params) { 
    var object_settings = this.settings;
    params.data[$(element).attr("name")] = value;
    $.post(params.url, params.data, function(response) {
        if (response == 'true'){ return true; }
        else {
            object_settings.messages[element.name] = response;
            return false;
        }
    }, 'text');
}, '');

It works...sort of....it sets the message, but doesn't display it initially (if you validate the field a second time, the message is displayed).

Any suggestions?

(maybe the remote rule offers this functionality...I couldn't find anything in the documentation)

Edit
Report

1 Answer

9

Was looking for solution to this as well, and found this...

In the original example, if you change this line:

object_settings.messages[element.name] = response;

To this:

$.validator.messages.duplicate = response;

This works for me. I found it here: http://blogs.teamb.com/craigstuntz/2009/01/15/37923/

answered 2011-01-11T04:08:47.793

Your Answer