简体   繁体   English

ASP.NET MVC验证-如何更改MVC验证引擎输出的html?

[英]ASP.NET MVC validation - How can I change the html that is output by the validation engine of MVC?

When using ASP.NET MVC validation on forms, error messages are output in specific html. 在表单上使用ASP.NET MVC验证时,错误消息以特定的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. 在上面的代码中,span标记已由验证框架自动生成,并且名为input-validation-error的类已添加到文本框。 But what if I want the validator to generate different html? 但是,如果我希望验证程序生成不同的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. 我想使用MVC验证框架,但是我想完全控制验证消息的显示方式,特别是因为MVC承诺控制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. 我设计的HTML和CSS(甚至在决定要使用的服务器端编程语言之前)对于错误消息是不同的,因为我想使用文本框的容器而不是文本框本身来设置类。 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. 尽管我对错误消息有一个跨度,但我想使用名为error的css类设置容器标签(dl)。 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. 注意:尽管我考虑过使用jQuery解决方案,该解决方案可以检测具有“ input-validation-error”类的表单字段,并使用我的自定义css类“ error”设置其各自的容器,但我认为这不是一个好的解决方案,并且我认为这是本来可以做得更好的解决方法。 I can upgrade from MVC 2 to MVC 3 if this solves the issue. 如果可以解决问题,我可以从MVC 2升级到MVC 3。

Sounds like you need to write a different extension method that edits the content of theirs 听起来您需要编写其他扩展方法来编辑其内容

namespace HtmlExtensions
{
    public static HtmlString CustomValidateFor(this HtmlHelper helper, ...)
    {
        return Edit(helper.ValidateFor(...))
    }
}

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 the field-validation-error class. 您可能会追寻的一个角度是利用ASP.NET MVC data-valmsg-for="inputName"验证脚本如何使用data-valmsg-for="inputName"属性搜索表单中的所有元素,并使用field-validation-error类对其进行标记。 So, you can work with the existing functionality by adding that attribute to your container: 因此,可以通过将该属性添加到容器中来使用现有功能:

<dl data-valmsg-for="FromEmail" data-valmsg-replace="false">
  <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> 
  </dd>
</dl>

Then, the <dl> will get the field-validation-error class in addition to the <input> . 然后, <dl>除了<input>还将获得field-validation-error类。 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 with data-valmsg-summary="true" and placing a <ul> in it). 如果您使用的是验证摘要,则可能不是问题(可以通过使用data-valmsg-summary="true"标记容器并在其中放置<ul>来控制该容器)。

If working within the framework doesn't work for you, you can always tweak the jquery.validate.unobtrusive.js to your liking. 如果在框架内无法正常工作,则可以随时根据自己的喜好调整jquery.validate.unobtrusive.js。 The error styling/placement code is pretty simple once you get your head around it (specifically, look at the onError and onErrors functions at line 39 and 55). 一旦掌握了它,错误样式/放置代码就非常简单(具体来说,请看第39和55行的onErroronErrors函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM