当我想验证其表单时,我在文本框字段上收到此错误消息。此字段有一个 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/