如何使KendoUI Validator忽略隐藏的表单元素? [英] How do I get KendoUI Validator to ignore hidden form elements?

查看:233
本文介绍了如何使KendoUI Validator忽略隐藏的表单元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将KendoUI Validator与ASP.NET WebForms项目一起使用. 我有一个简单的页面,其中包含许多输入,并且ASP.NET当然也添加了一些隐藏的表单元素.

I am attempting to use KendoUI Validator with an ASP.NET WebForms project. I have a simple page, that has a number of inputs, and of course ASP.NET adds some hidden form elements as well.

我有以下问题:

  1. 为什么KendoUI验证程序不忽略隐藏的表单字段,以及如何将其获取?
  2. 为什么KendoUI会将规则应用于每个输入字段,以及如何使规则忽略某些字段.我想要一种声明式的方式来执行此操作,而不是按照KendoUI Validator API页面中的示例在验证规则中添加各种异常.
  3. 难道如果输入元素中未设置任何规则作为属性(例如,必填),则不应用验证吗?

我得到的行为:

  • 在输入元素上完全没有验证特定属性的情况下,当我调用.validate()时仍然会应用验证规则
  • 隐藏的表单元素已通过验证.

我正在使用以下剑道:

http://cdn.kendostatic.com/2013.2.716/js/jquery.min.js
http://cdn.kendostatic.com/2013.2.716/js/kendo.all.min.js
http://cdn.kendostatic.com/2013.2.716/styles/kendo.common.min.css
http://cdn.kendostatic.com/2013.2.716/styles/kendo.default.min.css

我整理了一个小提琴来演示这一点: http://jsfiddle.net/codeowl/B5ML4/3/

I have put together a fiddle that demonstrates this: http://jsfiddle.net/codeowl/B5ML4/3/

这是代码,用于那些无法访问小提琴的人:

And here is the code, for those that don't have access to fiddle:

我有以下标记:

<form action="/" id="testForm">
    <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
    <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

    <input type="text" id="testInput" value="">
    <a id="testValidate" href="javascript:;">Validate</a>
</form>

和以下脚本:

var validatable = $("#testForm").kendoValidator({
    rules: {
        testRule1: function (input) {
            // Only "Tom" will be a valid value for the FirstName input
            return input.is("[name=firstname]") && input.val() === "Tom";
        },
        testRule2: function (input) {
            return $.trim(input.val()) !== "";
        }
    },
    messages: {
        testRule1: "Your name must be Test",
        testRule2: "Your name must be Foo"
    }
}).data("kendoValidator");

$("#testValidate").click(function () {
    if (validatable.validate()) {
        alert('passed');
    }
});

当我按下验证链接时,它会显示隐藏字段的验证消息.

and when I press the validate link it shows validation messages for the hidden fields.

推荐答案

对于感兴趣的任何人,我最终都得到了对该问题的答复.我不得不将其发布在KendoUI Premium论坛上,以便有人回应.

For anyone interested, I did eventually get a response to this question. I had to post it on the KendoUI Premium Forums to get someone to respond.

以下是响应: 如何让KendoUI验证程序忽略隐藏的表单元素?

实际上,隐藏的输入元素通过验证传递 由于存在多个小部件,因此默认情况下规则逻辑 其中包含隐藏输入作为标记的一部分.但是,由于 内置规则根据某些属性的存在进行中继(如果存在) 丢失,隐藏的输入将不会进行任何验证.所以, 您自己的自定义规则应处理这种情况,并跳过 适当的元素.例如:

Indeed, the hidden input elements are passed through the validation rules logic by default due to the fact that there are multiple widgets which has a hidden input as part of there markup. However, as the built-in rules relays on the presence of certain attributes, if there are missing no validation will happen on the hidden inputs. Therefore, your own custom rules should handle this scenario and skip the appropriate elements. For example:

testRule2: function (input) {
    if (!input.is(":hidden")) {
        return $.trim(input.val()) !== "";
    }
    return true;
}

这篇关于如何使KendoUI Validator忽略隐藏的表单元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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