private static void ApplyConventionsToValidationAttributes(IEnumerable<Attribute> attributes, Type containerType, string propertyName, Type defaultResourceType)
{
foreach (ValidationAttribute validationAttribute in attributes.Where(a => (a as ValidationAttribute != null)))
{
if (string.IsNullOrEmpty(validationAttribute.ErrorMessage))
{
string attributeShortName = validationAttribute.GetType().Name.Replace("Attribute", "");
var resourceType = validationAttribute.ErrorMessageResourceType ?? defaultResourceType;
//most extensive eg ClassName_PropertyName_Required
string resourceKey = GetResourceKey(containerType, propertyName) + "_" + attributeShortName;
if (!resourceType.PropertyExists(resourceKey))
{
//properytname only eg PropertyName_Required
resourceKey = propertyName + "_" + attributeShortName;
if (!resourceType.PropertyExists(resourceKey))
{
//most generic error, eg Error_Required
resourceKey = "Error_" + attributeShortName;
if (!resourceType.PropertyExists(resourceKey))
{
continue;
}
}
}
validationAttribute.ErrorMessageResourceType = resourceType;
validationAttribute.ErrorMessageResourceName = resourceKey;
}
}
}