我正在尝试使用 MVC Foolproof 库添加自定义验证器。
但是,按照说明 here我收到错误消息“系统未定义”。
我怀疑这是因为该库最初是为了与 MVC 2(也可能是 3)附带的旧验证脚本文件一起使用而编写的,所以有没有办法为附带的 javascript 验证文件注册我的验证函数MVC 4?
我的模型上的属性定义为
[RatingTextRequired(ErrorMessageResourceType = typeof(ModelResources.Customer.Order.FeedbackRating), ErrorMessageResourceName = "RequiredError_Comment")]
public string FeedbackText { get; set; }
验证属性定义为
public class RatingTextRequiredAttribute : ModelAwareValidationAttribute
{
//this is needed to register this attribute with foolproof's validator adapter
static RatingTextRequiredAttribute()
{
Register.Attribute(typeof (RatingTextRequiredAttribute));
}
public override bool IsValid(object value, object container)
{
var model = (Areas.Customer.Models.FeedbackRating) container;
return !(String.IsNullOrWhiteSpace(value.ToString()) && (model.WantsPostEditing || model.Rating == "0"));
}
}
JavaScript 连接是
Sys.Mvc.ValidatorRegistry.validators["ratingtextrequired"] = function (rule) {
return function(value, context) {
var wantsPostEditingProp = foolproof.getId(context.fieldContext.elements[0], "WantsPostEditing");
var wantsPostEditingVal = document.getElementById(wantsPostEditingProp).value;
var ratingProp = foolproof.getId(context.fieldContext.elements[0], "Rating");
var ratingVal = document.getElementById(ratingProp).value;
return !(value.length == 0 && (wantsPostEditingVal || ratingVal === "0"));
};
};
此外,我不确定需要什么文件 MvcFoolproofJQueryValidation.js,因为不知何故,我在没有此脚本文件的情况下进行了开箱即用的验证。遗憾的是,包含它对解决我的“Sys is not Defined”错误没有帮助。
最佳答案
根据您的错误,您正在使用 jquery 阻塞方法。
查看此post了解更多信息。
关于javascript - 添加自定义验证时出现"Sys is not defined"错误(MVC万无一失的验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21529839/