如何删除默认的客户端验证器? [英] How to remove default client-side validators?

查看:83
本文介绍了如何删除默认的客户端验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已基于以下主题中接受的答案实现了下拉式DateTime控件和验证器:如何验证日期使用3个下拉菜单(日,月,年)使用jquery非侵入式验证?

I have implemented a dropdown DateTime control and validators based on the accepted answer in the following topic: How to validate a date using 3 dropdowns( day, month, year ) using jquery unobtrusive validation?

我正在使用MVC 4.

I am using MVC 4.

此3下拉日期在大多数情况下都可以正常工作,但是有时当我单击这三个下拉菜单并选择不同的值(包括未选择的"值)时,会收到错误消息,形式为默认的DateTime验证器出生日期字段必须为日期."即使我选择了有效的年/月/日并且我的自定义验证器通过了.

This 3-dropdown date works fine mostly, but sometimes when I click those three dropdowns and choose different values (including "unselected" values), I get the error message form default DateTime validator "The field Date of birth must be a date." even if I have selected valid year/month/date and my custom validator passes.

正确设置日期后,此消息不会阻止发布表单,但对于用户而言,此消息会令人困惑.

This message does not prevent POSTing the form when the date is set correctly, but the message is confusing for users.

当我检查Firebug中的不干扰规则时,我看到此消息来自默认的日期"规则.我知道我可以使用规则(删除"(或删除规则,消息)来删除此默认日期规则,但是我不确定何时应该删除它们以停止MVC重新添加该规则.

When I inspect the unobtrusive rules in Firebug, I see that this message comes form default "date" rule. I know I could use rules( "remove" (or delete rule, message) to remove this default date rule, but I am not sure, when it is appropriate time to remove them to stop MVC adding that rule back again.

如何删除此默认的日期"规则,以确保只有我的自定义规则对这三个下拉列表有效?

How do I remove this default "date" rule to ensure that only my custom rule is effective for these three dropdowns?

推荐答案

我没有尝试过,但是我猜默认的数据类型客户端验证规则是由 ClientDataTypeModelValidatorProvider 添加的.默认情况下,此提供程序为ON.您可以尝试将其从Global.asax.cs中的 ModelValidatorProvidersCollection 中删除.

I haven't tried this but I guess the default datatype client validation rules are added by the ClientDataTypeModelValidatorProvider. This provider is ON by default. You can try removing that from the ModelValidatorProvidersCollection in Global.asax.cs.

  var clientDataTypeProvider = ModelValidatorProviders.Providers.FirstOrDefault(p => p.GetType().Equals(typeof(ClientDataTypeModelValidatorProvider)));
  ModelValidatorProviders.Providers.Remove(clientDataTypeProvider);

注意:通过删除此选项,您还将失去对数字的验证.另一个选择是创建一个自定义 ClientDataTypeModelValidatorProvider 并将其添加到providers集合中.还有一件事是,这个想法在全球范围内起作用,并影响到所有其他地方.

Note: By removing this you will lose the validation for number as well. Other option is create a custom ClientDataTypeModelValidatorProvider and add it into the providers collection. One more thing is this idea works at a global level and affect all the other places.

这篇关于如何删除默认的客户端验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆