MVC3 不显眼的验证将验证移至自定义元素

发布于 2024-12-10 15:00:06 字数 509 浏览 0 评论 0原文

在服务器端我渲染一个隐藏字段,然后使用一个名为 Flexbox 的 jquery 小部件创建一个组合框,它在客户端创建一个输入元素,并在您在框中选择某些内容后将所选 ID(不是文本)复制到隐藏字段。

问题是,当验证出现问题时,验证代码会向隐藏字段添加一个类名,我希望将其添加到输入元素中,我可以以某种方式监听添加类名的时间,或者某种方式挂钩到事件并移动输入字段的类名。

这可行,但它很难看,想要一个更好的解决方案,

var oldClass = $hdn.attr('class');

setInterval(function () {
    if (oldClass != $hdn.attr('class')) {
        $input.removeClass(oldClass);
        oldClass = $hdn.attr('class');
        $input.addClass($hdn.attr('class'));
    }
}, 200);

谢谢。

Serverside I render a hiddenfield, I then use a jquery widget called flexbox to create a combobox, it creates a input element client side and copies the selected ID (Not text) to the hidden field once you select something in the box.

The problem is that the validation code adds a classname to the hiddenfield when something is wrong with validation, I want it to be added to the input element, can I somehow listen to when the classname is added, or somehove hook into the event and move the classname to the inputfield.

This works but its ugly as hell, would like a better solution

var oldClass = $hdn.attr('class');

setInterval(function () {
    if (oldClass != $hdn.attr('class')) {
        $input.removeClass(oldClass);
        oldClass = $hdn.attr('class');
        $input.addClass($hdn.attr('class'));
    }
}, 200);

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽梦紫曦~ 2024-12-17 15:00:06

在验证隐藏元素的地方,我添加了一个自定义属性 data-val-visibleid。然后,在 jquery.validate.js 中,我修改了 highlightunhighlight 函数,在两个函数的末尾添加了以下内容

if ($(element).is(":hidden")) {
    var targetId = $(element).attr("data-val-visibleid");
    $("#" + targetId).addClass(errorClass).removeClass(validClass);
}

:不喜欢干预 jquery.validate.js,但它通常是实现此类自定义的最简单方法。

更新

我做了一些研究,发现 jquery.validate 有一个漂亮的 setDefault 方法,您可以在其中覆盖默认函数,例如highlight() 和 unhighlight。加载其他脚本后,将以下内容添加到您的页面:

$.validator.setDefaults( {
    highlight: function (element, errorClass, validClass) {
        $(element).addClass(errorClass).removeClass(validClass);
        if ($(element).is(":hidden")) {
            var targetId = $(element).attr("data-val-visibleid");
            $("#" + targetId).addClass(errorClass).removeClass(validClass);
        }
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).removeClass(errorClass).addClass(validClass);
        if ($(element).is(":hidden")) {
            var targetId = $(element).attr("data-val-visibleid");
            $("#" + targetId).addClass(errorClass).removeClass(validClass);
        }
    }
});

这将覆盖默认函数,而不更改底层脚本。

Where I have a hidden element being validated, I add a custom attribute, data-val-visibleid. Then, in jquery.validate.js, I modify the highlight and unhighlight functions by adding the following at the end of both functions:

if ($(element).is(":hidden")) {
    var targetId = $(element).attr("data-val-visibleid");
    $("#" + targetId).addClass(errorClass).removeClass(validClass);
}

Some people do not like to meddle in jquery.validate.js, but it is usually the easiest method to achieve customizations like this.

UPDATE

I did some research, and discovered that jquery.validate has a nifty setDefault method, where you can override the default functions, such as highlight() and unhighlight. Add the following to your page after the other scripts have been loaded:

$.validator.setDefaults( {
    highlight: function (element, errorClass, validClass) {
        $(element).addClass(errorClass).removeClass(validClass);
        if ($(element).is(":hidden")) {
            var targetId = $(element).attr("data-val-visibleid");
            $("#" + targetId).addClass(errorClass).removeClass(validClass);
        }
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).removeClass(errorClass).addClass(validClass);
        if ($(element).is(":hidden")) {
            var targetId = $(element).attr("data-val-visibleid");
            $("#" + targetId).addClass(errorClass).removeClass(validClass);
        }
    }
});

This will override the default functions, without changing the underlying script.

°如果伤别离去 2024-12-17 15:00:06

感谢 Counsellorben,我找到了一个很好的解决方案,不过我的方式略有不同。
首先,我重写主对象构造函数中的默认方法,该构造函数是在 document.ready 中构造的。然而 document.ready 为时已晚,当从 form.valid() 进行触发验证时,您的方法不会触发,但是在提交时会触发(非常奇怪)此代码既适用于提交,也适用于从脚本触发

(function() {
    var highlight = $.validator.defaults.highlight;
    var unhighlight = $.validator.defaults.unhighlight;

    $.validator.setDefaults({
        highlight: function (element, errorClass, validClass) {
            if ($(element).attr("data-val-visualId") != null) {
                element = $("#" + $(element).attr("data-val-visualId"))[0];
            }
            highlight(element, errorClass, validClass);
        },
        unhighlight: function (element, errorClass, validClass) {
            if ($(element).attr("data-val-visualId") != null) {
                element = $("#" + $(element).attr("data-val-visualId"))[0];
            }
            unhighlight(element, errorClass, validClass);
        }
    });
})();

Thanks to Counsellorben i found a good solution, I did it in a slightly different way though.
First i override the default methods in my master object contructor which is is constructed at document.ready. document.ready is however too late and your methods will not trigger when doing a triggering validation from form.valid() it will however trigg when doing a submit (very strange) this code works both for submit and triggered from script

(function() {
    var highlight = $.validator.defaults.highlight;
    var unhighlight = $.validator.defaults.unhighlight;

    $.validator.setDefaults({
        highlight: function (element, errorClass, validClass) {
            if ($(element).attr("data-val-visualId") != null) {
                element = $("#" + $(element).attr("data-val-visualId"))[0];
            }
            highlight(element, errorClass, validClass);
        },
        unhighlight: function (element, errorClass, validClass) {
            if ($(element).attr("data-val-visualId") != null) {
                element = $("#" + $(element).attr("data-val-visualId"))[0];
            }
            unhighlight(element, errorClass, validClass);
        }
    });
})();
绿萝 2024-12-17 15:00:06

我发现这两个答案都非常有帮助,只是想为使用 1.9.0 版验证插件的任何人添加您需要覆盖忽略隐藏字段的默认行为,如另一篇文章中详细介绍的: jQuery Validate - 启用隐藏字段验证

I found both these answers to be very helpful and just wanted to add for anyone using version 1.9.0 of the Validation plugin that you will need to override the default behavior that ignores hidden fields as detailed in this other post: jQuery Validate - Enable validation for hidden fields

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文