如何在字段上重新启用 ASP.NET MVC 不显眼的验证

发布于 2024-12-10 12:55:02 字数 618 浏览 1 评论 0原文

我有一个设置了不显眼的验证的表单,并且在大多数情况下都运行良好。然而,在某些情况下,我必须禁用表单中的选择元素,然后有时会重新启用该元素(主要是当 QA 尝试每种可能的情况但仍然如此时)。发生这种情况时,选择元素上的“必需”验证规则将不再起作用。

我尝试从元素的规则集合中删除并重新添加所需的规则,但无济于事。我尝试连接并调用描述的验证扩展 here 这也不起作用。

虽然我很想知道问题的根本原因,但我现在也愿意接受黑客攻击和解决方法。我有一种感觉这是一个已解决的问题,但我在搜索中没有看到它。

谢谢, 马修

更新:我很想选择一个答案,而不是承认我的愚蠢错误,但这并不会让网络变得更好。发生的事情是,选择元素中的选项是基于另一个字段的动态选项。当基于其他字段的选项中没有可进入的项目时,并且重新启用选择时,会重新填充选项,从而发生禁用。当我重新创建满足“必需”规则的选项列表时,我所做的就是在默认选项中添加“0”值。我将默认值更改为空字符串,问题就消失了。

处理实际问题的协议是什么?我应该关闭它吗?

I have a form with unobtrusive validation set up and all works well under most circumstances. However there are cases where I have to disable a select element in the form and then sometimes the element gets re-enabled (Mostly when QA is trying every possible circumstance but still). When this happens the "required" validation rule on the select element no longer works.

I have tried removing and re-adding the required rule from the element's rules collection to no avail. I have tried wiring in and calling the validation extension described here which also does not work.

While I would love to know the root cause of the issue I am also open at this point to hacks and workarounds. I have a feeling this is a solved problem but I am not seeing it in my searches.

Thanks,
Matthew

UPDATE: I am tempted to just select an answer and not fess up to my silly mistake, but that doesn't make the web better. What was going on is that the options in the select element are dynamic based on another field. The disabling happens when there are not items to go in the options based on that other field and when re-enabling the select the options are populated freshly. What I was doing was putting a "0" value in the default option when I recreated the options list which satisfied the "required" rule. I changed the default value to empty string and the issue went away.

What is the protocol for dealing with a question that isn't what it appeared to be? Should I close it?

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

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

发布评论

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

评论(2

睫毛溺水了 2024-12-17 12:55:02

为了重新解析不显眼的验证属性,您必须首先清除首次解析属性时创建的验证器和不显眼的验证数据。然后,您可以重新解析,如下所示:

$("form").removeData("validator");  
$("form").removeData("unobtrusiveValidation");  
$.validator.unobtrusive.parse("form");  

In order to re-parse unobtrusive validation attributes, you must first clear validator and unobtrusiveValidation data created when the attributes were first parsed. Then, you can re-parse, like so:

$("form").removeData("validator");  
$("form").removeData("unobtrusiveValidation");  
$.validator.unobtrusive.parse("form");  
怀念你的温柔 2024-12-17 12:55:02

如果您调用会发生什么

$.validator.unobtrusive.parse('#formId');

如果我没记错的话,应该对任何具有不显眼的 data- 属性(属于 #formId 的子元素)的元素再次启用不显眼的验证。

What happens if you call

$.validator.unobtrusive.parse('#formId');

If I recall correctly, that should enable unobtrusive validation again on any elements that have unobtrusive data- attributes that are children of #formId.

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