Skip to content

Instantly share code, notes, and snippets.

@danesparza
Created May 16, 2011 19:03
Show Gist options
  • Save danesparza/975079 to your computer and use it in GitHub Desktop.
Save danesparza/975079 to your computer and use it in GitHub Desktop.
ASP.NET MVC2 custom validation helper - C#
/// <summary>
/// If the given model field has validation errors, this will emit the given CSS class name
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="htmlHelper"></param>
/// <param name="expression"></param>
/// <param name="cssClassToEmit"></param>
/// <returns></returns>
public static MvcHtmlString ValidationCSSClassFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string cssClassToEmit)
{
MvcHtmlString htmlString = null;
// Figure out the expression text from the LambdaExpression using our nifty helper
// (thank God for RedGate reflector or I would have never figured this one out)
string expressionText = ExpressionHelper.GetExpressionText((LambdaExpression) expression);
// Get the full field name from the expression string:
string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
// Get the model state from the field name:
ModelState modelState = htmlHelper.ViewData.ModelState[fullHtmlFieldName];
// Check to see if we have errors. If we don't, just get out:
ModelErrorCollection errors = (modelState == null) ? null : modelState.Errors;
ModelError error = ((errors == null) || (errors.Count == 0)) ? null : errors[0];
if((error == null))
{
return null;
}
// At this point, we have errors -- but if we don't have a CSS class we're supposed to emit
// ... well, we're just SOL
if(!string.IsNullOrEmpty(cssClassToEmit))
{
htmlString = MvcHtmlString.Create(cssClassToEmit);
}
return htmlString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment