与参数ASP.NET MVC 3客户端验证 [英] ASP.NET MVC 3 client-side validation with parameters

查看:96
本文介绍了与参数ASP.NET MVC 3客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个职位<一个继href=\"http://stackoverflow.com/questions/4747184/perform-client-side-validation-for-custom-attribute/4782235#4782235\">Perform客户端验证为自定义属性

我试图让我围绕如何做到这一点的头,传递更多的参数给客户端脚本

I am trying to get my head around how to do this, passing additional parameters to the client-side script

据我了解,到目前为止,实现自定义的验证与MVC 3要求如下

As I understand it so far to implement custom validation with MVC 3 the following is required

根据ValidationAttribute和实施IClientValidatable。我也看到一些例子,从ModelValidator,这似乎同时实现ValidationAttribute和IClientValidatable的功能派生。所以这是我的困惑第一个点作为对diffirences是什么或者是否ModelValidator在MVC 2使用,但现在是很precated还是什么?

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 ?

ModelClientValidationRule的实例必须从GetClientValidationRules()来指定详细信息,如错误信息返回,ValidationType(我的理解是JavaScript函数,将执行名称在客户端验证)和任何其他自定义参数该属性可具有,并且需要被传递给Javascript验证

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.

我假定运行时(不知道它的哪一部分),然后使用 ModelClientValidationRule 来生成的标记元素HTML属性如下:

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]

实施客户端验证逻辑

JavaScript函数,必须创建并添加到jQuery.validators与jQuery.validators.addmethod(),这样jQuery是意识到这一点时,它需要执行。是这样的:

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   */ ; 
    },
    ''
); 

我在这里的问题是,是否签名功能(价值元素,则params)是将要处理的验证方法标准,我相信它会被一些jQuery的功能在适当的时候被调用,如提交表单之前或当元件失去fuces或KEYUP事件。我只是不undertand你如何控制好这个即选择哪个事件是appropriete为YOUT自定义验证。

My question here is whether the signature 'function (value, element, params)' is standard for methods that will handle validation and I assume it will be called by some jQuery functionality at the appropriate time such as before a form is submitted or when an element looses fuces or on keyUp events. I just don't undertand how you can controll this i.e. choose which event is appropriete for yout custom validation.

这意味着不显眼的属性;东西我不是很清楚,但认为这是一个jQuery的规则,但我不是如何将这些工作清楚。类似

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 (options) {
        options.rules['greaterThan'] = true;
        options.messages['greaterThan'] = options.message;
    }
); 

在这里,我的问题是关于'功能(选件)。这是将之前被调用函数功能(值,元素,则params)',并负责提取不显眼标记成可由jQuery.Validation可以理解的数据结构。从code例如,在我看来这是选择同时包含,从标签的属性值(如options.message)和jQuery的相关属性,必须映射到(如options.messages ['的对象ClientSideValidationFunctionName'。如果是的话如何自定义参数检索和映射。

My question here is about 'function (options)'. Is this the function that will be called before 'function (value, element, params)' and is responsible 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 options is an object that contains both, the attribute values from the tag (such as options.message) and the jQuery relevant properties it must map to (such as options.messages['ClientSideValidationFunctionName']. If so how are custom parameters retrieved and mapped.

我希望我没有添加任何额外的混乱。

I hope I have not added any additional confusion.

推荐答案

您可以使用<$c$c>ValidationParameters属性添加自定义参数的规则:

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 (options) {
        var param1 = options.params.param1; // shall equal 'value1'
        var param2 = options.params.param2; // shall equal 'value2'
        // TODO: use those custom parameters to define the client rules
    }
);


更新:

由于在评论部分要求在这里是你如何可以通过这些参数,以自定义验证规则功能:

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 (options) {
        // simply pass the options.params here
        options.rules['greaterThan'] = options.params;
        options.messages['greaterThan'] = options.message;
    }
);

jQuery.validator.addMethod('greaterThan', function (value, element, params) {
    // params here will equal { param1: 'value1', param2: 'value2' }
    return ...
}, '');

这篇关于与参数ASP.NET MVC 3客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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