MVC.NET 中的 xVal 与 nHibernate.validator 不会触发客户端验证

发布于 2024-08-05 14:54:25 字数 1202 浏览 13 评论 0原文

我有一个使用 NHibernate 和 NHibernate.Validator 的 ASP.NET MVC 项目,我想使用 xVal 1.0 (最新版本)。 我将请求的脚本添加到项目中并在 Site.Master 中引用它:

    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.3.2.js")%>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery.validate.js")%>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/xVal.jquery.validate.js")%>"></script>

然后我将 NHibernateValidatorRulesProvider 添加到 Global.asax Application_OnStart() 中的 xVal.ActiveRuleProviders:

xVal.ActiveRuleProviders.Providers.Add(new NHibernateValidatorRulesProvider(ValidatorMode.OverrideXmlWithAttribute));

最后,在视图上,我添加了:

<%= Html.ClientSideValidation<DomainModel.Entities.MyCustomClass>(Prefix) %>

我在IE 中,但 Firefox 中没有。如果现在输入一些无效字段并提交这些字段,xVal 不会触发 - 而是进行回发,并且服务器端验证会处理数据。

我做错了什么?我已经能够使 xVal 示例项目与 xVal 1.0 一起运行...

//编辑:我必须精确说明我的声明:实际上正在发生一些事情:它会进行回发并将无效字段标记为红色(就像它正在做的那样)始终使用服务器端验证)但是如果我在这些字段中写入一些内容,颜色就会变回正常状态。但是,如果我再次删除该值,则什么也不会发生,直到我单击“提交”并且表单执行另一个回发...如果我删除客户端验证代码,这似乎不会发生。

I have an ASP.NET MVC Project working with NHibernate and NHibernate.Validator and i'd like to use xVal 1.0 (most recent release).
I Added the requested scripts to the project and referenced it in Site.Master:

    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.3.2.js")%>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery.validate.js")%>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/xVal.jquery.validate.js")%>"></script>

Then I Added the NHibernateValidatorRulesProvider to the xVal.ActiveRuleProviders in the Global.asax Application_OnStart():

xVal.ActiveRuleProviders.Providers.Add(new NHibernateValidatorRulesProvider(ValidatorMode.OverrideXmlWithAttribute));

Finally, on the View, I added:

<%= Html.ClientSideValidation<DomainModel.Entities.MyCustomClass>(Prefix) %>

I get a JavaScript Warning in IE but none in Firefox. If enter now some invalid fields and submit these, xVal does not fire - a PostBack is being made instead and the Serverside validation processes the the data.

What am I doing wrong? I've been able to make the xVal sample Project running with xVal 1.0...

//edit: I have to precise my statement: Something is actually happening: It does a post-back and marks the invalid fields red (as it's doing all the time with the server-side validation) but if i write something into these fields, the color changes back to it's normal state. but if I then delete again the value, nothing is happening until I click Submit and the form does anothe postback... If I remove the clientsidevalidation code, this does not appear to happen.

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

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

发布评论

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

评论(2

烟雨扶苏 2024-08-12 14:54:25

确保您的验证器不允许空值,MVC 正在根据回发中的空字段验证表单,但 xVal 可能不会在每种情况下都这样做。

Make sure that your validator is not allowing nulls, MVC is validating forms against empty fields in postback but xVal might not do it in every case.

一绘本一梦想 2024-08-12 14:54:25

已解决:
我对文本字段使用了 [Length(1,50)]-验证器来确保它们包含一个值。但显然,xVal 不会处理此验证器,但例外:xVal 允许回发,即使字段为空。为了确保 xVal 检查该字段是否为空,我必须选择 [NotEmpty()] 属性!

SOLVED:
I used the [Length(1,50)]-Validator for the TextFields to make sure that they contain a value. But obviously, xVal does not handle this Validator as excepted: xVal allows a postback, even if the Fields are empty. To make sure that xVal checks if the field is empty, I had to choose the [NotEmpty()] attribute!

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