Last active
January 8, 2016 11:15
-
-
Save lkaczanowski/39497d7bf6ac50b10864 to your computer and use it in GitHub Desktop.
Fluent Validation helper methods to get errors for specific property
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; | |
using System.Linq.Expressions; | |
using System.Resources; | |
using System.Web.Mvc; | |
using FluentValidation.Results; | |
namespace Helpers | |
{ | |
public static class FluentValidationHelper | |
{ | |
// set your own resources | |
private static readonly ResourceManager DefaultResourceManager = ValidationMessagesResources.ResourceManager; | |
public static string GetErrorMessageByPropertyName<T>(this ValidationResult validationResult, Expression<Func<T, object>> property) | |
{ | |
return GetErrorMessageByPropertyName(validationResult, property, DefaultResourceManager); | |
} | |
public static string GetErrorMessageByPropertyName<T>(this ValidationResult validationResult, Expression<Func<T, object>> property, ResourceManager resourceManager) | |
{ | |
string propertyName = GetPropertyName(property); | |
var validationFailure = validationResult.Errors.FirstOrDefault(x => x.PropertyName == propertyName); | |
return validationFailure == null ? null : GetErrorMessage(validationFailure); | |
} | |
public static JsonResult GetJsonErrorMessageByPropertyName<T>(this ValidationResult validationResult, Expression<Func<T, object>> property) | |
{ | |
return GetJsonErrorMessageByPropertyName(validationResult, property, DefaultResourceManager); | |
} | |
public static JsonResult GetJsonErrorMessageByPropertyName<T>(this ValidationResult validationResult, Expression<Func<T, object>> property, ResourceManager resourceManager) | |
{ | |
var errorMessage = GetErrorMessageByPropertyName(validationResult, property, resourceManager); | |
object data = errorMessage == null ? true : (object)errorMessage; | |
return new JsonResult | |
{ | |
Data = data, | |
ContentType = null, | |
ContentEncoding = null, | |
JsonRequestBehavior = JsonRequestBehavior.AllowGet | |
}; | |
} | |
private static string GetPropertyName<T>(Expression<Func<T, object>> property) | |
{ | |
var memberExpression = property.Body as MemberExpression; | |
if (memberExpression != null) | |
{ | |
return memberExpression.Member.Name; | |
} | |
var unaryExpression = property.Body as UnaryExpression; | |
if (unaryExpression != null) | |
{ | |
memberExpression = unaryExpression.Operand as MemberExpression; | |
if (memberExpression != null) | |
{ | |
return memberExpression.Member.Name; | |
} | |
} | |
throw new InvalidOperationException("Coudln't get argument name from expression."); | |
} | |
private static string GetErrorMessage(ValidationFailure validationFailure) | |
{ | |
var validationData = validationFailure.CustomState as ValidationData; | |
return validationData != null | |
? ValidationMessagesResources.ResourceManager.GetString(validationData.ResourceKey) | |
: validationFailure.ErrorMessage; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment