BoC.Web.Mvc.MetaData.ExtraModelMetadataProvider.ApplyConventionsToValidationAttributes C# (CSharp) Method

ApplyConventionsToValidationAttributes() private static method

private static ApplyConventionsToValidationAttributes ( IEnumerable attributes, Type containerType, string propertyName, Type defaultResourceType ) : void
attributes IEnumerable
containerType System.Type
propertyName string
defaultResourceType System.Type
return void
        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;
                }
            }
        }