如何在 MVC3/jQuery 非侵入性验证中抑制客户端验证消息?
我使用 System.ComponentModel.DataAnnotations.RequiredAttribute 来指示需要某个模型属性。
在不滚动我自己的必需验证属性的情况下,是否有一种相当简单的方法来抑制客户端上的验证消息,同时仍然允许它们在通过 Html.ValidationSummary() 触发服务器端时显示?我仍然希望进行验证,甚至突出显示输入元素,但我不希望针对此特定的必需属性显示“xyzField is required”消息。
我确实需要 ValidationMessageFor 才能对该模型属性上的所有其他 ValidationAttributes 保持可操作性 - 这只是我想抑制的必需属性。
我的推理是这样的:我有几个靠近的小字段,并且必需的消息太过杀伤力和太繁忙。特别是考虑到“必需”是最常见的违规者。很明显,该字段为空,但却是必填字段(具有 * 必填符号,留空时会以红色突出显示)。
有什么想法吗?我知道自定义验证器并不困难 - 我已经有一些验证器,但我想了解是否有一种方法可以控制我不知道的这种行为。
I am using the System.ComponentModel.DataAnnotations.RequiredAttribute to indicate that a certain model property is required.
Without rolling my own Required validation attribute, is there a reasonably simple way to suppress validation messages on the client-side while still allowing them to show up when triggered server-side via Html.ValidationSummary()? I still want the validation to occur and even highlight the input element, but I don't want a "xyzField is Required" message to appear for this specific Required attribute.
I do need the ValidationMessageFor to remain operational for all other ValidationAttributes on this model property - it's only the Required one that I'd like to suppress.
My reasoning is thus: I have several small fields in close proximity, and the Required messages are overkill and too busy. Especially given that "Required" is the most frequent offender. It's quite clear that the field is blank, yet required (has * required symbol, plus highlights in red when left empty).
Any ideas? I know a custom validator isn't difficult - I have a few of them already, but I'd like to learn if there's a way to control this behavior that I am unaware of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该取出所有帮助程序的验证消息,例如:
然后将验证摘要帮助程序保留在表单顶部:
如果您需要更具体的答案,则必须发布代码。
在模型中将错误消息留空怎么样?例如,在您的必需属性中,它将是
[Required]
或[Required(ErrorMessage="")]
请参阅此帖子:模型外部验证
You should take out all the Validation Message For helper like:
And then leave the validation summary helper at the top of your form:
You would have to post the code if you need a more specific answer.
What about leaving the error message blank in the model? For example in your required attribute it would be
[Required]
or[Required(ErrorMessage="")]
See this post: Validation outside model