Orchard CMS DataAnnotations - 客户端验证
我试图在 Orchard 模块中进行不显眼的客户端验证,但遇到了问题。
在此示例中,我只是尝试使用 Html.TextBoxFor() 在文本框字段上强制执行RequiredAttribute。
在我看来,Orchard 实现的自定义 ModelValidatorProvider (LocalizedModelValidatorProvider) 会阻止呈现 HTML5 输入属性,特别是:
data-val
data-val-required
对于标准 DataAnnotations requiredAttribute。 jQuery.validate.unobtrusive 需要这些属性才能工作。
LocalizedModelValidatorProvider 将RequiredAttribute 映射到LocalizedRequiredAttribute,因此在为用LocalizedRequiredAttribute 修饰的模型属性渲染文本框输入时,这可能是Orchard ViewEngine 中的一个错误(或未实现的功能)?
我怀疑不知何故这并没有发生:(
tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
来自 System.Web.Mvc.Html.InputExtensions)
我目前正在运行 Orchard 1.3.9。
注意:一个 hacky 解决方法是删除 OrchardStarter 模块中 LocalizedModelValidatorProvider 的注册,并默认返回到标准 MVC 3 提供程序,尽管我希望尽可能不要干扰 Orchard 源(更不用说我有时可能需要本地化消息)...
I'm trying to get unobtrusive client-side validation working within an Orchard module, but i've hit a problem.
In this example i'm just trying to enforce the RequiredAttribute on a textbox field, using Html.TextBoxFor().
It looks to me as though the custom ModelValidatorProvider implemented by Orchard (LocalizedModelValidatorProvider) prevents the HTML5 input attributes from being rendered, specifically:
data-val
data-val-required
for the standard DataAnnotations RequiredAttribute. These attributes are required by jQuery.validate.unobtrusive to work.
The LocalizedModelValidatorProvider maps a RequiredAttribute to a LocalizedRequiredAttribute, so perhaps this a bug (or unimplemented feature) in the Orchard ViewEngine when rendering a textbox input for a model property decorated with a LocalizedRequiredAttribute?
I suspect that somehow this isn't happening:
tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
(from System.Web.Mvc.Html.InputExtensions)
I'm currently running Orchard 1.3.9.
Note: A hacky workaround is to remove the registration of the LocalizedModelValidatorProvider in the OrchardStarter module and default back to the standard MVC 3 provider, although i'm keen not to disturb the Orchard source if at all possible (not to mention i might need localized messages at some point)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我花了 3 周的时间解决了 MyPrettyCMS 中对话框中使用的不显眼的验证问题。
我还使用 LocalizedRequiredAttribute,正如您在那些自动生成的元数据模型中看到的那样。
我不了解 Orcad,但我认为它像我一样将用户表单呈现为 JQuery 对话框。
有两个困难:
第一:您必须将对话框字段(重新)附加到用于将数据发送到服务器的表单。
第二:您必须(重新)解析表单以获得有效的不显眼的验证
您将在这里找到完整的 基于 JQuery 对话框的表单 使用不显眼的验证并聚焦行 $.validator.unobtrusive.parse(form);
这使得分析 ajax 添加的控件变得不那么引人注目。
在JQuery常用工具框中查找$.fn.jqDialogFunction
,然后查看方法 $.fn.SaveContent -->$("#divStructurePage").parent().appendTo(form);
I spend 3 weeks to solve the problem of unobstrusive validation used in dialog boxes in MyPrettyCMS.
I also use LocalizedRequiredAttribute as you can see in those AutoGenerated MetaDataModels.
I don't know Orcad but I presume it presents user forms as JQuery Dialogs as I do.
There is two difficulties :
1 st : you have to (re)attach dialog fields to the form you will use to send data to the server.
2 nd : you have to (re)parse form to obtain a working unobstrusive validation
You will find here a complete JQuery Dialog based Form working with unobstrusive validation and focus the line $.validator.unobtrusive.parse(form);
This force unobstrusive to analyze ajax added controls.
Look $.fn.jqDialogFunction in the JQuery common tool box
Then Look the method $.fn.SaveContent -->$("#divStructurePage").parent().appendTo(form);
在编辑器模板视图中添加此代码
在模块“Scripts”文件夹中添加“jquery.validate.min.js,jquery.validate.unobtrusive.min.js”。
在 Web.cofig 文件中添加以下设置。
Add this code in your Editor Template View
Add "jquery.validate.min.js,jquery.validate.unobtrusive.min.js" in your module "Scripts" folder.
Add the below setting in Web.cofig file.