添加自定义验证时出现“未定义Sys”错误(MVC Foolproof验证)

时间:2014-02-03 14:25:24

标签: javascript validation asp.net-mvc-4 foolproof-validation

我尝试使用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没有帮助没有定义"错误。

1 个答案:

答案 0 :(得分:0)

根据您的错误,您使用的是jquery阻塞方法。

有关详细信息,请参阅此post

相关问题