/// <summary> /// Gets the metadata for the specified property. /// </summary> /// <returns> /// The metadata for the property. /// </returns> /// <param name="attributes">The attributes.</param> /// <param name="containerType">The type of the container.</param> /// <param name="modelAccessor">The model accessor.</param> /// <param name="modelType">The type of the model.</param> /// <param name="propertyName">The name of the property.</param> protected override ModelMetadata CreateMetadata([NotNull] IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { List <Attribute> newAttributes = null; if (LocalizationConventions.Enabled && containerType != null && !string.IsNullOrEmpty(propertyName)) { var defaultResourceType = LocalizationConventions.GetDefaultResourceType(containerType); if (defaultResourceType != null) { newAttributes = new List <Attribute>(); DisplayAttribute displayAttribute = null; foreach (var attribute in attributes) { if (attribute is ValidationAttribute) { ValidationAttributeTransformer.Transform((ValidationAttribute)attribute, containerType, propertyName, defaultResourceType); newAttributes.Add(attribute); } else if (attribute is DisplayAttribute) { displayAttribute = attribute as DisplayAttribute; } else { newAttributes.Add(attribute); } } // ensure DisplayAttribute and clone existing displayAttribute = displayAttribute.Copy() ?? new DisplayAttribute(); MvcExtensions.DisplayAttributeTransformer.Transform(displayAttribute, containerType, propertyName, defaultResourceType); newAttributes.Add(displayAttribute); } } var metadata = base.CreateMetadata(newAttributes ?? attributes, containerType, modelAccessor, modelType, propertyName); DisplayNameTransformer.Transform(metadata); return(metadata); }
/// <summary> /// Populates the error message from the given metadata. /// </summary> /// <param name="validationAttribute"></param> public void PopulateErrorMessage([NotNull] ValidationAttribute validationAttribute) { Invariant.IsNotNull(validationAttribute, "validationMetadata"); string errorMessage = null; if (ErrorMessage != null) { errorMessage = ErrorMessage(); } if (!string.IsNullOrEmpty(errorMessage)) { validationAttribute.ErrorMessage = errorMessage; } else if (ErrorMessageResourceType != null && !string.IsNullOrEmpty(ErrorMessageResourceName)) { validationAttribute.ErrorMessageResourceType = ErrorMessageResourceType; validationAttribute.ErrorMessageResourceName = ErrorMessageResourceName; } else if (LocalizationConventions.Enabled) { // enables support for partial matadata if (ErrorMessageResourceType != null) { validationAttribute.ErrorMessageResourceType = ErrorMessageResourceType; } if (!string.IsNullOrEmpty(ErrorMessageResourceName)) { validationAttribute.ErrorMessageResourceName = ErrorMessageResourceName; } var conventionType = modelMetadata.With(m => m.ContainerType); var propertyName = modelMetadata.With(m => m.PropertyName); var defaultResourceType = LocalizationConventions.GetDefaultResourceType(conventionType); ValidationAttributeTransformer.Transform(validationAttribute, conventionType, propertyName, defaultResourceType); } }