ASP.NET MVC 验证 - 如何更改 MVC 验证引擎输出的 html?
在表单上使用 ASP.NET MVC 验证时,错误消息会以特定的 html 输出。例如,如果您正在验证必须填写的文本框,则验证失败后的输出将如下所示:
<label for="MonkeyName">Name: </label>
<span class="field-validation-error">*</span>
<br />
<input class="input-validation-error" id="MonkeyName" name="MonkeyName" type="text" value="" />
在上面的代码中,span 标记已由验证框架和名为 input-validation- 的类自动生成。错误已添加到文本框中。但是如果我希望验证器生成不同的 html 该怎么办?
我想使用 MVC 验证框架,但是我想完全控制验证消息的显示方式,特别是因为 MVC 承诺控制 UI。我设计的 HTML 和 CSS(甚至在决定要使用的服务器端编程语言之前)对于错误消息是不同的,因为我想使用文本框的容器而不是文本框本身设置一个类。以此为例:
<dl class="error">
<dt>
<label for="email">Email</label>
<span class="required">Required</span>
</dt>
<dd>
<input class="textinput_med" id="FromEmail" name="FromEmail" type="text" value="" />
<a href="#"><img src="Content/images/structure/help.png" width="30" height="30" alt="Help" /></a>
<span class="errormessage">Some Error</span>
</dd>
</dl>
虽然我有一个错误消息的范围,但我想使用名为 error 的 css 类设置容器标记 (dl)。有没有办法可以确定验证框架呈现错误消息的方式?
注意:虽然我考虑过使用 jQuery 的解决方案来检测具有“输入验证错误”类的表单字段,并使用我的自定义 css 类“错误”设置它们各自的容器,但我认为这不是一个好的解决方案,并且我认为这是一个可以做得更好的解决方法。如果这解决了问题,我可以从 MVC 2 升级到 MVC 3。
When using ASP.NET MVC validation on forms, error messages are output in specific html. Like for example if you are validating a textbox that must be filled in, the output after the validation fails would be like so:
<label for="MonkeyName">Name: </label>
<span class="field-validation-error">*</span>
<br />
<input class="input-validation-error" id="MonkeyName" name="MonkeyName" type="text" value="" />
In the above code, the span tag has been automatically generated by the validation framework and a class named input-validation-error was added to the textbox. But what if I want the validator to generate different html?
I would like to use the MVC validation framework however I would like to have total control on the way validation messages are displayed especially since MVC promises control over the UI. The HTML and CSS I have designed (before even deciding the server-side programming language to be used) is different for error messages because I want to set a class with the container of the textbox and not the textbox itself. Take this as an example:
<dl class="error">
<dt>
<label for="email">Email</label>
<span class="required">Required</span>
</dt>
<dd>
<input class="textinput_med" id="FromEmail" name="FromEmail" type="text" value="" />
<a href="#"><img src="Content/images/structure/help.png" width="30" height="30" alt="Help" /></a>
<span class="errormessage">Some Error</span>
</dd>
</dl>
Although I have a span for the error message, I would like to set the container tag (dl) with the css class named error. Is there a way I can determine the way error messages are rendered by the validation framework?
Note: Although I have thought about a solution with jQuery that detects form fields that have a class of 'input-validation-error' and sets their respective containers with my custom css class 'error' I don't think it's a good solution and I think it's a workaround for something that could have been better. I can upgrade from MVC 2 to MVC 3 if this solves the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您需要编写一个不同的扩展方法来编辑它们的内容
Sounds like you need to write a different extension method that edits the content of theirs
您可以追求的一个角度是利用 ASP.NET MVC 不显眼的验证脚本如何使用
data-valmsg-for="inputName"
所有元素> 属性,并用field-validation-error
类标记它们。因此,您可以通过将该属性添加到容器来使用现有功能:然后,
除了获得之外还将获得
field-validation-error
类<输入>
。这里的缺点是您会丢失验证错误消息。如果您使用的是验证摘要,也许这不会成为问题(您可以通过使用data-valmsg-summary="true"
标记容器并放置来控制该容器>
中)。
如果在框架内工作不适合您,您可以随时根据自己的喜好调整 jquery.validate.unobtrusive.js。一旦您了解了错误样式/放置代码,它就非常简单(具体来说,请查看第 39 行和第 55 行的
onError
和onErrors
函数)。One angle you could pursue would be taking advantage of how the ASP.NET MVC unobtrusive validation script searches for all elements within the form with a
data-valmsg-for="inputName"
attribute and it tags them with thefield-validation-error
class. So, you can work with the existing functionality by adding that attribute to your container:Then, the
<dl>
will get thefield-validation-error
class in addition to the<input>
. The drawback here is that you lose the validation error message. If you're using a validation summary, maybe that wouldn't be an issue (and you can control that container by tagging a container withdata-valmsg-summary="true"
and placing a<ul>
in it).If working within the framework doesn't work for you, you can always tweak the jquery.validate.unobtrusive.js to your liking. The error styling/placement code is pretty simple once you get your head around it (specifically, look at the
onError
andonErrors
functions at line 39 and 55).