Created
December 31, 2012 08:22
-
-
Save SyntaxC4/4418269 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq.Expressions; | |
| using System.Text.RegularExpressions; | |
| using System.Web.Mvc; | |
| using System.Web.Mvc.Html; | |
| public static class ValidationExtensions | |
| { | |
| public static MvcHtmlString ValidationMessageLabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string errorClass = "error") | |
| { | |
| string elementName = ExpressionHelper.GetExpressionText(expression); | |
| MvcHtmlString normal = html.ValidationMessageFor(expression); | |
| if (normal != null) | |
| { | |
| string newValidator = Regex.Replace(normal.ToHtmlString(), @"<span([^>]*)>([^<]*)</span>", string.Format("<label for=\\"{0}\\" $1>$2</label>", elementName), RegexOptions.IgnoreCase); | |
| if (!string.IsNullOrWhiteSpace(errorClass)) | |
| newValidator = newValidator.Replace("field-validation-error", errorClass); | |
| return MvcHtmlString.Create(newValidator); | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment