【问题标题】:Warning: No message defined for警告:没有为
【发布时间】:2014-08-18 10:31:42
【问题描述】:

当我想验证其表单时,我在文本框字段上收到此错误消息。 此字段有一个 requiredif 验证属性,但此字段是其原始字段的克隆。我改变了它的所有属性,甚至它的规则也被复制了。

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 代码:

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 代码:

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);

很遗憾,我找不到错误。有人可以帮帮我吗?

编辑: cloe 方法中缺少这一行:

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

EDIT2:
现在效果很好。
感谢本站:
http://xhalent.wordpress.com/2011/02/08/copying-jquery-validation-from-one-element-to-another/

【问题讨论】:

    标签: jquery asp.net-mvc-4 custom-validators


    【解决方案1】:

    您可以传递多条消息。像这样使用 $.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;
    
    });
    

    【讨论】:

      【解决方案2】:

      我刚遇到这个。 另一种方法是覆盖 jQuery 验证。由于没有向我的自定义验证提供消息,我得到了这个。

      var errorMessage = "something";
      jQuery.validator.addMethod("ruleName", function(value, element, errorMessage){
          //code to validate, return bool
      });
      

      在我的标记中,我将类“ruleName”添加到属性中

      <input id="someInput" class="ruleName"></input>
      

      两种方式都有效。

      另外,请将您的答案标记为正确,以便其他用户可以找到此帖子已回答。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-03
        • 2021-12-05
        • 2012-02-24
        • 2023-01-03
        • 2021-11-25
        • 2018-07-21
        相关资源
        最近更新 更多