添加临时的不引人注目的验证
我有一个使用 EF 的 MVC3 应用程序,其中一张表最终出现在许多视图模型中。它有一个通常不需要的字段。但是,有一种情况(和一种视图)需要它。我希望客户端和服务器都在需要该字段的地方验证该字段的必需性。
现在,我不想将 Required
数据注释放在字段上,因为这样它将始终是必需的。即使在未显示字段进行编辑的视图上也是如此,这将导致用户无法消失验证消息。
我也不是在寻找 RequiredIf
验证属性,因为这不符合设计。仅在一种情况下才需要该字段,并且我不想仅仅为了依赖关系而在所有视图模型中添加多余表中的额外字段。
我想要的只是能够以一种临时的、不基于 DataAnnotations 的方式为一个视图中的一个字段打开所需的字段验证。
我立即想到的方法是在为字段创建标记时指定必要的 data-val-*
属性(通过该字段末尾的 HtmlAttributes
对象) HtmlHelper.TextBoxFor
签名)。如果我理解正确的话,这应该提示 JS 不显眼的验证按要求处理该字段。
但在我看来,这……有点太过了。不引人注目的验证支持会侵入更高级别的代码。有没有更优雅的方法来做到这一点?我可以使用 jQuery 验证函数调用来实现我想要发生的事情吗?
I have an MVC3 app, using EF, where one table winds up in a lot of view models. It has a field that is normally not required. But, there is one situation (and one view) where it is required. I want both client and server validation for this field's required-ness, in the one place where it IS required.
Now, I do not want to put the Required
data annotation on the field, because then it will always be required. This would be true even on views where the field isn't displayed for edit, which would result in a validation message the user couldn't make go away.
I also am not looking for a RequiredIf
validation attribute, because that doesn't fit the design. The field is only required under one circumstance, and I don't want to have to stick in an extra field from a superfluous table in all my view models, just for the sake of the dependency.
All I want is to be able to turn on required field validation for one field in one view, in an ad-hoc, not-based-on-DataAnnotations manner.
The way that immediately suggested itself to me was to specify the necessary data-val-*
attributes when creating the markup for the field (through the HtmlAttributes
object at the end of the HtmlHelper.TextBoxFor
signature). If I understand correctly, this should cue JS unobtrusive validation to treat the field as required.
But this is ... a little too much of a hack, seems to me. Unobtrusive validation support would be intruding into the higher-level code. Is there a more elegant way to do this? A jQuery Validation function call I can use to make what I want to have happen, happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种数据验证方法不仅有代码味道,而且也不可靠,因为最终用户可能使用不进行不引人注目的验证的客户端。客户端验证是糖果,拥有它真是太好了。您必须始终进行服务器端验证。
这是当您将数据模型用于视图模型时遇到的问题。正确的方法是为每个视图创建单独的视图模型。您可以通过共享视图模型并仅为那些不同的视图创建单独的视图模型来完成。无论哪种情况,如果您有一个独特的视图,它就需要自己独特的视图模型。
Not only does that data-val approach have a codes smell, it's also unreliable because the end user may be using a client that doesn't do unobtrusive validation. client-side validation is candy, that's nice to have. You must always have server-side validation.
This is the problem you have when you use your data model for your view model. The correct approach is to create seperate view models for each view. You can get by with sharing View Models and creating seperate view models for only those views that are different. In either case, if you have a unique view, it needs its own unique view model.