MVC4定制不显眼的验证器不工作 [英] MVC4 custom unobtrusive validator isn't working

查看:101
本文介绍了MVC4定制不显眼的验证器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道什么是错的。语法似乎是正确的....但它仍然不会触发客户端。如果我提交表单,我得到的服务器端验证,客户端没有什么...

not sure what is wrong. Syntax seems correct.... but it still doesn't fire on client side. If I submit the form, I get server side validation, client side nothing...

下面是code,它是在页面上:

Here is the code that is on the page:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript">
    // we add a custom jquery validation method
    (function ($) {
        $.validator.addMethod('additive', function (value, element, params) {
        //just return false to test it.
        return false;
    });
    // and an unobtrusive adapter
    $.validator.unobtrusive.adapters.add("additive", ["field2", "field3", "field4"], function (options) {
        var params = {
            field2: options.params.field2,
            field3: options.params.field3,
            field4: options.params.field4
        };
        options.rules['additive'] = params;
        if (options.message) {
            options.messages['additive'] = options.message;
        }
    });
}) (jQuery);
</script>

下面是上是与客户端(IClientValidatable)验证程序的部分:

Here is the part that is on the validator that is related to client side (IClientValidatable):

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "additive",
                ErrorMessage = "ERROR MESSAGE"
            };

            rule.ValidationParameters.Add("field2", propName2);
            rule.ValidationParameters.Add("field3", propName3);
            rule.ValidationParameters.Add("field4", propName4);

            yield return rule;
        }

该模型装饰如下:

The model is decorated as following:

[SumValidation("OtherField2...")]
public int MyField { get; set; }

在现场呈现,它都在那里,所有从服务器端的东西在数据XXX属性方面。就在这个特定的客户端验证不火。任何人看到我错过了什么?

When field renders, it is all there, all the stuff from the server side in terms of data-xxx attributes. Just this specific client validation does not fire. Anyone see what I'm missing?

推荐答案

想通了。如果有人运行到这一点。新增自定义验证页面上的太晚了。当我提出我的自定义验证的javascript到_Layout.cshtml的头部就开始工作。

figured it out. If anyone runs into this. Added custom validation too late on the page. After I moved my custom validation javascript to the head section of the _Layout.cshtml it started to work.

所以,如果你的脚本看起来正确,良好的地方,检查。

So if your script looks right, good place to check.

另一个解决办法是运行$ .validator.unobtrusive.parse(形式);它重新加载所有验证程序。

Another work around is to run $.validator.unobtrusive.parse('form'); which reloads all the validators.

这篇关于MVC4定制不显眼的验证器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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