C# 为什么验证器会接受我的旧错误消息而不是新的错误消息?
我使用的是MVC5,它包含一个具有以下字段的表单:C# 为什么验证器会接受我的旧错误消息而不是新的错误消息?,c#,javascript,jquery,asp.net-mvc,validation,C#,Javascript,Jquery,Asp.net Mvc,Validation,我使用的是MVC5,它包含一个具有以下字段的表单: StudentType - Radio button GradId - dropdown HSYearofGradId - dropdown CollegeStatusId - dropdown 对于这些字段,我希望根据它们选择的内容,在HSYearofGradId上应用验证规则。例如,如果他们选择了一个StudentTypea,他们将只看到GradId,只有在GradId中选择了第三项,他们才会看到HSYearofGradId。同样,如果他
StudentType - Radio button
GradId - dropdown
HSYearofGradId - dropdown
CollegeStatusId - dropdown
对于这些字段,我希望根据它们选择的内容,在HSYearofGradId
上应用验证规则。例如,如果他们选择了一个StudentType
a,他们将只看到GradId
,只有在GradId
中选择了第三项,他们才会看到HSYearofGradId
。同样,如果他们选择StudentType
B,则他们会同时看到GradId
和HSYearofGradId
。这里的关键是StudentType
A和B是不同的,因此错误消息应该显示不同的值
我收到以下错误消息:您必须为{0}年选择一个选项才能继续。”
我想在客户端用一个值替换{0}
在遍历JavaScript之后,我注意到我的字符串一开始被替换,但随后被我设置的原始错误覆盖:必须为{0}年选择一个选项才能继续。我在一个验证器的内部注意到了这一点:
$.validator.format(message.replace(theregex,{$1}”),rule.parameters);`将{0}更改为[object]
我使用以下问题作为参考:
因此,我使用了MVC的低调客户端验证功能。我创建了一个自定义验证属性,实现了IClientValidatable接口。GetClientValidationRules方法如下所示:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var modelClientValidationRule = new ModelClientValidationRule
{
ValidationType = "requiredif",
ErrorMessage = ErrorMessage
};
// rule.ValidationParameters["condition"] = "#" + Condition; // CSS Selector (won't work if applied to nested properties on a viewmodel)
// Attached to field
modelClientValidationRule.ValidationParameters.Add("property", Property);
modelClientValidationRule.ValidationParameters.Add("condition", Condition);
modelClientValidationRule.ValidationParameters.Add("minimum", Minimum);
modelClientValidationRule.ValidationParameters.Add("errormessage", ErrorMessage);
yield return modelClientValidationRule;
}
[Display(Name = "College Student Status")]
[Integer(ErrorMessage = "You must select an option for College Student Status in order to proceed.")]
[RequiredIf("StudentType", "college", 1, ErrorMessage = "You must select an option for College Student Status in order to proceed.")]
public Nullable<int> CollegeStatusId { get; set; }
[Display(Name = "When will you graduate?")]
[Integer(ErrorMessage = "You must select an option for when will you graduate in order to proceed.")]
[RequiredIf("StudentType", "high school", 1, ErrorMessage = "You must select an option for when will you graduate in order to proceed.")]
public Nullable<int> GradId { get; set; }
[Display(Name = "HS Year")]
public string HighSchoolYear { get; set; }
// Choosing 3rd option for gradId
[Display(Name = "In which year did you graduate?")]
[Integer(ErrorMessage = "You must select an option for your year of graduation to proceed.")]
[RequiredIf("StudentType", "3", 1, ErrorMessage = "You must select an option for your year of {0} in order to proceed.")]
public Nullable<int> HSYearofGradId { get; set; }
jQuery.validator.addMethod('requiredif', function (value, element, params) {
var validator = this;
var conditionIsMet = false;
var propertyId = $(element).attr('data-val-requiredif-property');
var condition = $(element).attr('data-val-requiredif-condition');
var minimum = $(element).attr('data-val-requiredif-minimum');
var errorMessage = $(element).attr('data-val-requiredif-errormessage');
var errors = {};
var radioValue = $("input[name='" + propertyId + "']:checked").val();
if (propertyId === "StudentType") {
// i.e. high school
if (radioValue === condition) {
conditionIsMet = true;
}
}
else if (propertyId === "HighSchoolLocal") {
// what if not dropdown
if (value === "") {
// check if we are in no mode.
if (radioValue === condition) {
conditionIsMet = false;
} else {
if ($('#LocalBoroughId').is(':disabled')) {
conditionIsMet = false;
} else {
conditionIsMet = true;
}
}
}
}
//TODO: hardcoded value
if (element.id === "HSYearofGradId") {
if (parseInt($(element).val()) < parseInt(minimum)) {
var gradType;
if (radioValue === 'college') {
// TODO: error message is still wrong
gradType = "High School graduation";
} else {
gradType = "graduation";
}
errors[element.name] = $.validator.format(errorMessage, gradType);
$.validator.messages.requiredif = $.validator.format(errorMessage, gradType);
validator.showErrors(errors);
conditionIsMet = false;
} else {
conditionIsMet = true;
}
} else if (value) {
if (parseInt(value) < parseInt(minimum)) {
errors[element.name] = errorMessage;
validator.showErrors(errors);
conditionIsMet = false;
} else {
conditionIsMet = true;
}
}
return conditionIsMet; });
编辑:
我想我发现了这个问题。jquery.validate.js中有几个方法可以为给定的元素名和验证方法获取自定义消息。所有参数都传递给下面的方法。我注意到列表中的第一个错误被传递回了。而且,第一个错误恰好是实体上的原始错误集。
findDefined: function() {
for (var i = 0; i < arguments.length; i++) {
if ( arguments[i] !== undefined ) {
return arguments[i];
}
}
就我而言,我做了以下工作:
errors[element.name] = $.validator.format(errorMessage, gradType);
$.validator.messages.requiredif = $.validator.format(errorMessage, gradType);
this.settings.messages["HSYearofGradId"]["requiredif"] = $.validator.messages.requiredif;
this.settings.messages["FIELDNAME"]["VALIDATION_NAME"] = "NEW MESSAGE"
errors[element.name] = $.validator.format(errorMessage, gradType);
$.validator.messages.requiredif = $.validator.format(errorMessage, gradType);
this.settings.messages["HSYearofGradId"]["requiredif"] = $.validator.messages.requiredif;