Warning: No message defined for

Sándor Hatvani picture Sándor Hatvani · Aug 18, 2014 · Viewed 11.5k times · Source

I get this error message on a textbox field when I want to validate its form. There is a requiredif validation attribute on this field, but this field is a clone of its original. I changed all properties of it and even its rules also copied.

C#:

public class ModelClientValidationRequiredIfRule : ModelClientValidationRule
    {
      public ModelClientValidationRequiredIfRule(string errorMessage, 
                             string otherProperty, 
                             Comparison comparison, 
                             object[] value)
      {            
        ErrorMessage = errorMessage;
        ValidationType = "requiredif";
        ValidationParameters.Add("other", otherProperty);
        ValidationParameters.Add("comp", comparison.ToString().ToLower());
        StringBuilder sb = new StringBuilder();
        int valueLength = value.Length;
        int utolsoElem = valueLength - 1;
        for (int i = 0; i < valueLength; i++)
        {
            sb.Append(value[i].ToString());
            if (i != utolsoElem)
            {
                sb.Append("|");
            }
        }

        ValidationParameters.Add("value", sb.ToString().ToLower());
      }
    }

JavaScript code for initializing the validation:

jQuery.validator.unobtrusive.adapters.add("requiredif", ["other", "comp", "value"],
function (options) {
  options.rules['requiredif'] = {
      other: options.params.other,
      comp: options.params.comp,
      value: options.params.value
  };
  if (options.message) {
      options.messages['requiredif'] = options.message;
  }

} );

JavaScript code from clone method:

thisRaw.prev().find("td:eq(1) input")
    .attr("id", "Cert_" + numOfCer + "__EndDate")
    .attr("name", "Cert[" + numOfCer + "].EndDate")
    .attr("data-val-requiredif-other", "Cert[" + numOfCer + "].BizonyitvanyFajta");
var rules = $('form').find('#Cert_0__EndDate').rules();
rules['messages'] = $('form').data('validator').settings.messages["Cert[0].EndDate"];
thisRaw.prev().find("td:eq(1) input").rules("add", rules);
thisRaw.prev().find("td:eq(1) span").attr("data-valmsg-for", "Cert[" + numOfCer + "].EndDate");

$("#Cert_" + numOfCer + "__StartDate").removeClass("hasDatepicker");
$("#Cert_" + numOfCer + "__EndDate").removeClass("hasDatepicker");

CreateDynamicDatepicker(numOfCer);

Unfortunately, I cannot find the error. Can anybody help me, please?

EDIT: This line is missing from the cloe method:

rules['messages'] = $('form').data('validator').settings.messages["Cert[0].EndDate"];

EDIT2:
Now it works well.
Thanks to this site:
http://xhalent.wordpress.com/2011/02/08/copying-jquery-validation-from-one-element-to-another/

Answer

Shiva Manhar picture Shiva Manhar · Jun 1, 2018

You can pass multiple message. use like this $.validator.messages.[custom method name] ='Your message'.

jQuery.validator.addMethod("custom_method_name", function(value, element){

if(condition1)
{
$.validator.messages.custom_method_name = 'Your message1';
return false;
}    

if(condition1)
{
$.validator.messages.custom_method_name = 'Your message2';
return false;
}

return true;

});