ASP.NET MVC 2.0 自定义客户端验证

发布于 2024-09-12 04:44:18 字数 1852 浏览 2 评论 0原文

我正在尝试创建一个验证器,以确保至少选择 2 个项目。验证器在服务器端正常工作,但客户端代码永远不会被执行。

这是代码:

 Sys.Mvc.ValidatorRegistry.validators["country"] = function (rule) {

        var min = rule.ValidationParameters["min"];        

        return function (value, context) {

            if (value >= min) return true;

            return rule.ErrorMessage;

        };
    };  

这是验证器代码:

  public class CountryValidator : DataAnnotationsModelValidator<CustomValidations.CountryAttribute>
    {
        private int _minimum;
        private string _message; 

        public CountryValidator(ModelMetadata metadata, ControllerContext context, CustomValidations.CountryAttribute attribute) : base(metadata,context,attribute)
        {
            _minimum = attribute.Minimum;
            _message = attribute.ErrorMessage; 
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule()
            {
                ErrorMessage = _message,
                ValidationType = "country"
            };

            rule.ValidationParameters.Add("min", _minimum);
            return new[] { rule };
        }


    }

我什至在 global.asax 文件中注册了验证适配器:

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(AgeAttribute), typeof(AgeValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CountryAttribute),typeof(CountryValidator));
        }

我认为验证器仅适用于具有值属性(如文本框等)的元素。

更新 1:

EnableClientValidation 是正确调用并且所有必需的 JS 文件都包含在项目中。看来我需要将 onblur 附加到上下文中。我会尝试并发布结果。

I am trying to make a validator that will make sure that at least 2 items are selected. The validator works correctly on the server side but the client side code never gets executed.

Here is the code:

 Sys.Mvc.ValidatorRegistry.validators["country"] = function (rule) {

        var min = rule.ValidationParameters["min"];        

        return function (value, context) {

            if (value >= min) return true;

            return rule.ErrorMessage;

        };
    };  

And here is the validator code:

  public class CountryValidator : DataAnnotationsModelValidator<CustomValidations.CountryAttribute>
    {
        private int _minimum;
        private string _message; 

        public CountryValidator(ModelMetadata metadata, ControllerContext context, CustomValidations.CountryAttribute attribute) : base(metadata,context,attribute)
        {
            _minimum = attribute.Minimum;
            _message = attribute.ErrorMessage; 
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule()
            {
                ErrorMessage = _message,
                ValidationType = "country"
            };

            rule.ValidationParameters.Add("min", _minimum);
            return new[] { rule };
        }


    }

I have even registered the validation adapter in global.asax file:

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(AgeAttribute), typeof(AgeValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CountryAttribute),typeof(CountryValidator));
        }

I am thinking that the validator only works with the elements that have a value property like textboxes etc.

UPDATE 1:

EnableClientValidation is invoked correctly and all the required JS files are included in the project. It seems like I need to attach the onblur to the context. I will try that and post the results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想念有你 2024-09-19 04:44:18

<% =Html.EnableClientValidation(); %> 需要在你的视野中的某个地方。另请确保在同一视图中(在 js 函数之前)引用 MicrosoftAjax.jsMicrosoftMvcValidation.js

<% =Html.EnableClientValidation(); %> needs to be in your view somewhere. Also make sure you reference MicrosoftAjax.js and MicrosoftMvcValidation.js in the same view (before your js function).

凉城 2024-09-19 04:44:18

您缺少 MicrosoftMvcAjax.js 或者您需要在 jQuery 中实现自定义验证,如 Haack 先生的网站 http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

Either your missing MicrosoftMvcAjax.js or you need to implement your custom validation in jQuery as described on Mr. Haack's website http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx.

月棠 2024-09-19 04:44:18

我认为这是因为输入文本框的 onblur 事件调用了默认验证。对于列表框,不会引发此事件。

I think it is because the default validation is invoked on the onblur event of the input textbox. And for a listbox this event was not being thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文