ASP.ET MVC 客户端验证参数(ASP.ET MVC client
从这个帖子继续执行自定义属性的客户端验证
我正在努力解决如何做到这一点,将额外的参数传递给客户端脚本
据了解,到目前为止,使用MVC 实现定制验证需要以下功能
创建自定义验证属性
基于ValidationAttribute并实现IClientValidatable。 我还看到一些派生自ModelValidator的示例,它似乎实现了ValidationAttribute和IClientValidatable的功能。 所以这是我的第一个混乱的观点,就是在MVC 2中是否使用了ModelValidator,但是现在已经弃用了什么?
必须从GetClientValidationRules()返回ModelClientValidationRule的实例,以指定详细信息,例如错误消息ValidationType(我理解为将执行客户端验证的Javascript函数的名称)以及属性的任何其他自定义参数可能有,而且需要传递给Javascript验证。
我假设运行时(不确定它的哪一部分)然后使用ModelClientValidationRule在标签元素中生成html属性,如下所示:
data-val="true" (to indicate that the element requires validation) data-val-[ValidationType]=[ErrorMessage] data-val-[ValidationType].[ValidationParameters(n).Key]=[ValidationParameters(n).Value]实现客户端验证逻辑
必须创建一个Javascript函数,并使用jQuery.validators.addmethod()将其添加到jQuery.validators中,以便JQuery在需要执行时才知道它。 就像是:
jQuery.validator.addMethod( 'greaterThan', function (value, element, params) { /.../ return /* true or false */ ; }, '' );实现一个不引人注意的适配器
这将不引人注目的属性转化为 我不太清楚,但认为它是一个jQuery规则,但我不清楚这些工作如何。 就像是
jQuery.validator.unobtrusive.adapters.add( 'futuredate', { }, function (opti) { opti.rules['greaterThan'] = true; ['greaterThan'] = ; } );Create a custom validation attribute
Based on ValidationAttribute and implementing IClientValidatable. I have also see some examples deriving from ModelValidator, which seems to implement the functionality of both ValidationAttribute and IClientValidatable. So this is my first point of confusion as to what the diffirences are or whether ModelValidator was used in MVC 2 but is now deprecated or what ?
An instance of ModelClientValidationRule must be returned from GetClientValidationRules() to specify details such as the error message, ValidationType (which I understand to be the name of the Javascript function that will perform the client-side validation) and any additional custom parameters that the attribute may have, and that need to be passed to the Javascript validation.
I assume that the runtime (not sure which part of it) then use the ModelClientValidationRule to generate html attribute in the tag elements as follows:
data-val="true" (to indicate that the element requires validation) data-val-[ValidationType]=[ErrorMessage] data-val-[ValidationType].[ValidationParameters(n).Key]=[ValidationParameters(n).Value]Implement the client-side validation logic
A Javascript function must be created and added to jQuery.validators with jQuery.validators.addmethod() so that JQuery is aware of it when it need to be executed. Something like:
jQuery.validator.addMethod( 'greaterThan', function (value, element, params) { /.../ return /* true or false */ ; }, '' );Implement an unobtrusive adapter
This translates unobtrusive attributes to; something I am not very clear on, but assume it to be a jQuery Rule, but I am not clear on how those work. Something like
jQuery.validator.unobtrusive.adapters.add( 'futuredate', { }, function (opti) { opti.rules['greaterThan'] = true; ['greaterThan'] = ; } );My question here is about 'function (opti)'. Is this the function that will be called before 'function (value, element, params)' and is respible for extracting the unobtrusive tags into a data structure that can be understood by jQuery.Validation. From the code example it seems to me that opti is an object that contains both, the attribute values from the tag (such as ) and the jQuery relevant properties it must map to (such as ['ClientSideValidationFunctioname']. If so how are custom parameters retrieved and mapped.
I hope I have not added any additional confusion.
最满意答案
您可以使用ValidationParameters属性向规则添加自定义参数:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = this.ErrorMessage, ValidationType = "futuredate", }; rule.ValidationParameters.Add("param1", "value1"); rule.ValidationParameters.Add("param2", "value2"); yield return rule; }可用于适配器:
jQuery.validator.unobtrusive.adapters.add( 'futuredate', [ 'param1', 'param2' ], function (opti) { var param1 = opti.params.param1; // shall equal 'value1' var param2 = opti.params.param2; // shall equal 'value2' // TODO: use those custom parameters to define the client rules } );更新:
根据评论部分的要求,您可以将这些参数传递给自定义验证器规则函数:
jQuery.validator.unobtrusive.adapters.add( 'futuredate', [ 'param1', 'param2' ], function (opti) { // simply pass the opti.params here opti.rules['greaterThan'] = opti.params; ['greaterThan'] = ; } ); jQuery.validator.addMethod('greaterThan', function (value, element, params) { // params here will equal { param1: 'value1', param2: 'value2' } return ... }, '');You could use the ValidationParameters property to add custom parameters to the rule:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = this.ErrorMessage, ValidationType = "futuredate", }; rule.ValidationParameters.Add("param1", "value1"); rule.ValidationParameters.Add("param2", "value2"); yield return rule; }which could be used in the adapter:
jQuery.validator.unobtrusive.adapters.add( 'futuredate', [ 'param1', 'param2' ], function (opti) { var param1 = opti.params.param1; // shall equal 'value1' var param2 = opti.params.param2; // shall equal 'value2' // TODO: use those custom parameters to define the client rules } );UPDATE:
As requested in the comments section here's how you could pass those parameters to the custom validator rule function:
jQuery.validator.unobtrusive.adapters.add( 'futuredate', [ 'param1', 'param2' ], function (opti) { // simply pass the opti.params here opti.rules['greaterThan'] = opti.params; ['greaterThan'] = ; } ); jQuery.validator.addMethod('greaterThan', function (value, element, params) { // params here will equal { param1: 'value1', param2: 'value2' } return ... }, '');#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上一篇:一个比较全的vim指令记录
下一篇:大数据学习
推荐阅读
留言与评论(共有 12 条评论) |
本站网友 如何快速长高10厘米 | 26分钟前 发表 |
就像是: jQuery.validator.addMethod( 'greaterThan' | |
本站网友 中铁二局集团有限公司 | 24分钟前 发表 |
[ 'param1' | |
本站网友 主力进出指标 | 0秒前 发表 |
"value1"); rule.ValidationParameters.Add("param2" | |
本站网友 福州铁通宽带 | 21分钟前 发表 |
element | |
本站网友 黄岛区 | 6分钟前 发表 |
use those custom parameters to define the client rules } ); 更新: 根据评论部分的要求 | |
本站网友 国际俱乐部饭店 | 27分钟前 发表 |
function (opti) { var param1 = opti.params.param1; // shall equal 'value1' var param2 = opti.params.param2; // shall equal 'value2' // TODO | |
本站网友 带牛字的网名 | 4分钟前 发表 |
它似乎实现了ValidationAttribute和IClientValidatable的功能 | |
本站网友 chbtc | 25分钟前 发表 |
params) { /.../ return /* true or false */ ; } | |
本站网友 抵达 | 20分钟前 发表 |
ValidationType = "futuredate" | |
本站网友 莲花国际广场 | 27分钟前 发表 |
例如错误消息ValidationType(我理解为将执行客户端验证的Javascript函数的名称)以及属性的任何其他自定义参数可能有 | |
本站网友 人体内脏解剖图 | 8分钟前 发表 |
As requested in the comments section here's how you could pass those parameters to the custom validator rule function |