MVC 将方法添加到 jquery.validate.unobtrusive.js 中
我最近有一个关于让复选框验证工作的问题在 MVC 项目中的客户端。这个问题已经成功回答,但又提出了另一个疑问。
为了使我的复选框验证工作,我需要将以下 javascript 位直接添加到 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);
});
这效果很好,但我不高兴必须更改此文件,以防万一 Microsoft 或验证插件男孩们将来会更新该文件。如果我不再从事该项目,则该文件可能会被覆盖,而人们却没有意识到它已被定制。
因此,考虑到这一点,我尝试将其添加到外部 javascript 文件中:
$.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);
});
不幸的是,现在我的复选框上的客户端脚本无法运行。谁能看到我做错了什么吗?
提前
致谢
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.
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.
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?
Thanks in advance
S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗅探,
我越看越摇头(对自己)。
经过进一步审查,达林的方法将起作用,前提是您在他的页面脚本中添加一行:
每当您进行更改(例如添加新适配器)时,您必须重新解析不显眼的验证属性。由于
jquery.validate.unobtrusive.js
中的最后一个操作是解析属性,并且在解析之后添加适配器,因此重新解析可以解决此问题。辅导员
这解决了您的问题,但仍然没有解决如何添加其他自定义验证器的问题,这些验证器不使用 jquery.validate.js 中的内置方法而不修改 jquery.validate.unobtrusive。 js。
PPS 我找到了添加自定义验证方法的答案。为了在不修改
jquery.validate.unobtrusive.js
的情况下添加自定义验证方法,您需要“借用”它的一些代码来添加到您的页面脚本中。添加自定义方法如下所示:Sniffer,
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:
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
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 modifyingjquery.validate.unobtrusive.js
.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:不引人注目的验证只给我带来悲伤。
在一个非常非常简单的测试页中,这是有效的:
但是,当我将此代码移动到更复杂的表单时,它突然停止工作,我不知道为什么,也没有时间深入研究不引人注目的代码来尝试找出答案。
coachlorben 的解决方案以我更复杂的形式工作。
如果有人可以向我指出一个网站,该网站详细解释了如何正确地将自定义验证器添加到不引人注目的验证中,我将永远感激不已。我访问的每个网站都有不同的解决方案。
Unobtrusive validation is giving me nothing but grief.
In a very very simple test page, this works:
However, when I move this code to my more complex form, suddenly it stops working and I have no idea why and no time to delve into the unobtrusive code to try and find out.
counsellorben's solution works in my more complex form.
If anyone can point me to a site that explains in detail how to properly add a custom validator to unobtrusive validation, I will be forever greatful. Each site I visit has a different solution.
我可以毫无问题地将这段代码添加到外部 javascript 文件中,该文件是我从 此站点
您确定将脚本放在页面中的 jquery 脚本之后吗?我的是列表中的最后一个。
I have no problem adding this code to an external javascript file, which i pilfered from This site
Are you certain that you put your script AFTER the jquery scripts in your page? Mine is the last in the list.