地址字段的自定义 ASP.NET MVC 验证摘要
我正在尝试找出验证一页结账的最佳方法。 它包含:
- 发货地址、
- 账单地址
- 等。Address
类显然包含 First Name
、Last Name
、Street1
、Street2
>、城市
、州
、邮政编码
、电话
等。
假设用户在输入任何内容之前单击“确定” - 然后你最终会遇到十几个或更多的验证错误,给你一大块看起来很难看的红色文本。
我想将地址验证为单个实体,并给出智能错误 - 例如“不完整的地址”,或者在适当的时候给出更具体的错误。但我仍然希望能够突出显示存在问题的每个单独领域。我现在看不到执行此操作的简单方法,因为显然 Html.ValidationSummary
帮助程序将显示每个字段。
所以我想将摘要显示为:
"Your shipping address is incomplete"
并以红色 Zip
和 City
突出显示。
我想我必须做一个完全自定义的 ValidationSummary,甚至可能是一个完全自定义的数据结构。
是否有任何验证框架可以使这样的摘要更容易进行,其中摘要应该显示智能摘要,而不仅仅是每个单独的字段错误。
编辑:MVC 2 RC 现在支持模型级错误。
ValidationSummary 现在支持 仅模型级别的重载 显示错误。这很有用 如果您正在显示验证 每个表单旁边内嵌的消息 场地。此前,这些消息 将在验证中重复 概括。有了这些新的变化,您 可以让摘要显示 整体验证消息(例如“有 您的表单提交有误”) 以及验证列表 不适用于的消息 特定领域。
有人有如何做到这一点的实际示例吗?
I'm trying to figure out the best way to validate a one page checkout.
It contains :
- ship address
- billing address
- etc.
the Address class obvious contains First Name
, Last Name
, Street1
, Street2
, City
, State
, Zip
, Phone
etc.
Lets say the user clicks 'OK' before entering anything - then you end up with a dozen or more validation errors giving you a large block of red text that just looks ugly.
I'd like to validate the address as a single entity, and give an intelligent error - such as 'incomplete address', or more specific errors when appropriate. But I still want to be able to highlight each individual field that has a problem. I can't see an easy way to do this right now, because obviously the Html.ValidationSummary
helper will show every field.
So I want to show the summary as:
"Your shipping address is incomplete"
and highlight in red Zip
and City
.
I think I'd have to do a completely custom ValidationSummary, and maybe even a completely custom datastructure.
Do any validation frameworks make such a summary easier to do, where the summary should show an intelligent summary and not just every individual field error.
Edit: MVC 2 RC now supports model-level errors.
ValidationSummary now supports
overloads where only model-level
errors are displayed. This is useful
if you are displaying validation
messages inline next to each form
field. Previously, these messages
would be duplicated in the validation
summary. With these new changes, you
can have the summary display an
overall validation message (ex. “There
were errors in your form submission”)
as well as a list of validation
messages which don’t apply to a
specific field.
Anybody got an actual sample of how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用复合地址属性并将整个地址作为一个单元进行验证:
验证属性将如下所示:
You could go with a composite Address property and validate the whole address as a unit:
And the validation attribute would look something like this:
IDataErrorInfo 有两个成员:
如果您实现 Error 成员,您将收到一条错误消息。
IDataErrorInfo has two members:
If you implement Error member, you'll have one error message.
我在最近的项目中处理了类似的问题,我做了一个自定义的验证摘要,这里是代码:
我在部分视图中完成了它,但也许最好将其包装在 HTML Helper 方法中,就像原始的 ValidationSummary 一样。
您可以在里面检查是否有任何特殊和独特的要求。
希望有帮助。
I deal with a similar problem in a recent project, i did a custom Validation Summary, here is the code:
I did it in a Partial View, but maybe is better wrap this in a HTML Helper method, just like the original ValidationSummary.
Inside you can check for any special and unique requirements.
Hope it Helps.
我会这样做:
将验证错误放入 ModelState 中,无论哪种方式对您来说都是最舒服的。您可以使用 IDataErrorInfo 或使用 DataAnnotations 和验证运行器将它们直接添加到控制器中的 ModelState。只要您用错误填充 ModelState 并重新显示视图,这并不重要。
然后,确保您的所有输入也有一个相应的 Html.ValidationMessage() 与您的表单中的它们关联:
根据验证错误类的 css 规则,这会将文本框变成红色,并在其旁边显示一个红色星号,告诉您他们需要纠正他们的输入的用户。
最后,由于您对显示完整的验证摘要不感兴趣,因此只需执行一个简单的检查以查看 ModelState 是否有效,如果不有效,则显示您的通用消息。
该解决方案将突出显示用户错误填写的特定字段,并显示您想要的错误的简短描述。
Here is what I would do:
Put your validation errors into ModelState whichever way is the most comfortable to you. You can add them directly to ModelState in your controller, using IDataErrorInfo, or with DataAnnotations and a validation runner. It doesn't really matter as long as you populate ModelState with your errors and re-display the view.
Then, make sure all your inputs also have a corresponding Html.ValidationMessage() associated with them in your form:
Depending on your css rules for the validation error classes this will turn the text-box red and display a red asterisk next to it telling the user they need to correct their input.
Finally, since you're not interesting in displaying the full validation summary just do a simple check to see if ModelState is valid, and if not display your generic message.
This solution will highlight the specific fields that the user has incorrectly filled out and display a short description of the errors like you want.
Scottgu 刚刚发布了 关于新验证功能的精彩博客文章。
虽然它没有深入探讨如何实现模型级验证,但它指向默认的 ASP.NET MVC 2 应用程序项目模板作为如何执行此操作的说明:
Scottgu just released a great blog post on the new validation features.
While it doesn't go into depth on how to implement model-level validation it points to the default ASP.NET MVC 2 application project template as an explanation of how to do this: