views:

96

answers:

1

MVC.net 2 by default outputs validation messages like this:

<span id="UserName_validationMessage" class="field-validation-valid">A Validation message</span>

I would like it to do it like this:

<label id="UserName_validationMessage" class="field-validation-valid">A Validation message</label>

Is there a way to do it like the display and editor templates? Or is there another way to do it globally?

A: 

The code that generates that html is inside of ValidateExtensions.cs, in System.Web.Mvc. You could update the code to output a label instead of a span and then recompile that. The code looks like the following:

        TagBuilder builder = new TagBuilder("span");
        builder.MergeAttributes(htmlAttributes);
        builder.MergeAttribute("class", HtmlHelper.ValidationMessageCssClassName);
        builder.SetInnerText(String.IsNullOrEmpty(validationMessage) ? GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState) : validationMessage);

Otherwise, you might be able to override ValidationExtensions.ValidationSummary(this HtmlHelper htmlHelper, string message, IDictionary htmlAttributes) and do it that way...

Jamie