MVC添加方法到jquery.validate.unobtrusive.js [英] MVC Adding methods into jquery.validate.unobtrusive.js

查看:1085
本文介绍了MVC添加方法到jquery.validate.unobtrusive.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我对<一个问题href=\"http://stackoverflow.com/questions/6923430/mvc-unobtrusive-validation-on-checkbox-not-working/6931490#6931490\">getting复选框验证一个MVC项目中的客户端工作。这个问题已成功回答了,但提出了另一个查询。

I recently had a question on getting checkbox validation working on the client side within a MVC project. This question was successfully answered, but raised another query.

为了我的复选框的验证工作,我需要的JavaScript以下位直接添加jquery.validate.unobtrusive.js:

In order for my checkbox validation to work I needed to add the following bits of javascript directly into jquery.validate.unobtrusive.js:

$jQval.addMethod("mustbetrue", function (value, element, param) {
    // check if dependency is met
    if (!this.depend(param, element))
        return "dependency-mismatch";
    return element.checked;
});

adapters.add("mustbetrue", function (options) {
    setValidationValues(options, "mustbetrue", true);
});

这伟大的工作,但我不高兴不必更改这个文件,以防万一微软或验证插件男孩在将来更新文件。如果我还没有该项目的工作文件可在没有人意识到它已经定制覆盖。

this worked great, but I'm unhappy about having to change this file just in case Microsoft or the validation plugin boys update the file in the future. If I'm not still working on the project this file may be overwritten without people realising it's been customised.

所以考虑到这一点我尝试添加该到外部JavaScript文件:

So with that in mind I tried adding this into an external javascript file:

$.validator.addMethod("mustbetrue", function (value, element, param) {
    // check if dependency is met
    if (!this.depend(param, element))
        return "dependency-mismatch";
    return element.checked;
});

$.validator.unobtrusive.adapters.add("mustbetrue", function (options) {
    setValidationValues(options, "mustbetrue", true);
});

不幸的是,现在我的复选框的客户端脚本不运行。任何人都可以看到我在做什么错了?

Unfortunately now the client side script on my checkboxes doesn't run. Can anyone see what I'm doing wrong?

在此先感谢

取值

推荐答案

嗅探器,

我越是看这个,我越摇我的头(我自己)。

The more I look at this, the more I shake my head (at myself).

在进一步审核达林的方法著作,您添加一行到他的网页脚本:

Upon further review, Darin's method will work, provided that you add one line to his page script:

<script type="text/javascript">
    $.validator.unobtrusive.adapters.addBool("mustbetrue", "required");
    $.validator.unobtrusive.parse();
</script>

每当你做出改变(如添加新的适配器),则必须重新解析不显眼的验证属性。由于 jquery.validate.unobtrusive.js 的最后一个动作是属性的分析,并解析后,该适配器被添加,重新解析解决了这个问题。

Whenever you make a change (such as adding a new adapter), you must re-parse the unobtrusive validation attributes. Since the last action in jquery.validate.unobtrusive.js is the parsing of the attributes, and the adapter is being added after the parsing, re-parsing solves this issue.

counsellorben

counsellorben

P.S。这解决了您的问题,但还有许多有待解决的如何添加其他自定义验证不使用内置的 jquery.validate.js 方法,而无需修改<$ c中的问题$ C> jquery.validate.unobtrusive.js 。

P.S. This solves your issue, but still leaves unresolved the issue of how to add other custom validators which do not use built-in methods from jquery.validate.js without modifying jquery.validate.unobtrusive.js.

P.P.S。我发现添加自定义的验证方法的答案。为了添加自定义验证mmethods无需修改 jquery.validate.unobtrusive.js ,你需要借用一些code的添加到您的网页脚本。添加自定义方法,那么如下所示:

P.P.S. I found the answer for adding custom validation methods. In order to add custom validation mmethods without modifying jquery.validate.unobtrusive.js, you need to "borrow" some of its code to add to your page script. Adding a custom method then looks like the following:

<script type="text/javascript">
    var $jQval = $.validator,
        adapters,
        data_validation = "unobtrusiveValidation";

    function setValidationValues(options, ruleName, value) {
        options.rules[ruleName] = value;
        if (options.message) {
            options.messages[ruleName] = options.message;
        }
    }

    $jQval.addMethod("mustbetrue", function (value, element, param) {
        // check if dependency is met
        if (!this.depend(param, element))
            return "dependency-mismatch";
        return element.checked;
    });

    $.validator.unobtrusive.adapters.add("mustbetrue", function (options) {
        setValidationValues(options, "mustbetrue", true);
    });

    $jQval.unobtrusive.parse();
</script>

这篇关于MVC添加方法到jquery.validate.unobtrusive.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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