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)

5 Answers 5

15

Here's the solution....needed to call the showErrors function of the object:

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

Taken from "remote" in jquery.validate.js (lines 917 - 919)

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

1 Comment

The default error message is over writing the custom message, when I do this.
11

I have followed the process mentioned in the site http://blogs.teamb.com/craigstuntz/2009/01/15/37923/#comment-125774 and succeeded.

You have to call the method with the dynamic message, so that it will display that message. For example

$.validator.addMethod("validatePremium", function(value, element, param) {

    if( Condition )    

     {
       $.validator.messages.validatePremium = "your message here";
       //enter code here
       //...
       return false;
     }

    }, $.validator.messages.validatePremium);

1 Comment

Best answer for me, solve my issue with a charm: Link : codepen.io/plasebo/pen/VGaQam Thanks Kiran
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/

Comments

4

this worked for me

var errorMsg = '', $valid = false;
$.validator.addMethod("methodName",function(val, elem){
   $.ajax({
      url:'your_script.php',
      type:"POST",
      dataType:"json",
      data : {},
      success:function(response){
         if(response.success == false){
            errorMsg = response.msg;
            $valid = response.success;
         }
         else{
            $valid = true;
         }
      }
   });
   $.validator.messages["methodName"] = errorMsg;
   return $valid;
},'');

make sure to replace "methodName" with your method name in this case "duplicate" in both places(addMethod function 1st arg and in the addMethod function body $.validator.messages["methodName"])

Comments

0

You can customize the error message per validation method and per element, just as the plugin says:

// Adding validation method
$.validator.addMethod('sampleminimum', function (val, element, params) {
    return this.optional(element) || $(element).val() > params.min;
}, function(params, element) {
   return 'The field value cannot be less than %1'.replace('%1', params.min);
});

// Using validation
$(form).validate({
    rules: {
        'elementName': {
            sampleminimum: {min: 10}
        }
    }
});

Comments

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.