protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable <Attribute> attributes, Type containerType, Type modelType, string propertyName) { var theAttributes = attributes.ToList(); var prototype = base.CreateMetadataPrototype(theAttributes, containerType, modelType, propertyName); foreach (var validationAttribute in theAttributes.OfType <ValidationAttribute>().Where(a => !string.IsNullOrWhiteSpace(a.ErrorMessage))) { try { prototype.AdditionalValues.Add(validationAttribute.GetHashCode().ToString(CultureInfo.InvariantCulture), validationAttribute.ErrorMessage); } catch (Exception) { // there is weird cases when item has been added to the Dictionary already.. // TODO: need to investigate more about this } } // handle also case when [Display] attribute is not present if (containerType?.GetCustomAttribute <LocalizedModelAttribute>() == null) { return(prototype); } var translation = ModelMetadataLocalizationHelper.GetTranslation(containerType, propertyName); prototype.DisplayName = translation; if (prototype.IsRequired && ConfigurationContext.Current.ModelMetadataProviders.MarkRequiredFields && ConfigurationContext.Current.ModelMetadataProviders.RequiredFieldResource != null) { prototype.DisplayName += LocalizationProvider.Current.GetStringByCulture(ConfigurationContext.Current.ModelMetadataProviders.RequiredFieldResource, CultureInfo.CurrentUICulture); } var displayAttribute = theAttributes.OfType <DisplayAttribute>().FirstOrDefault(); if (!string.IsNullOrEmpty(displayAttribute?.Name)) { displayAttribute.Name = translation; } if (!string.IsNullOrEmpty(displayAttribute?.Description)) { prototype.Description = ModelMetadataLocalizationHelper.GetTranslation(containerType, $"{propertyName}-Description"); } return(prototype); }
protected override ModelMetadata CreateMetadata( IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { var theAttributes = attributes.ToList(); var data = base.CreateMetadata(theAttributes, containerType, modelAccessor, modelType, propertyName); if (containerType == null) { return(data); } if (containerType.GetCustomAttribute <LocalizedModelAttribute>() == null) { return(data); } data.DisplayName = !ModelMetadataLocalizationHelper.UseLegacyMode(data.DisplayName) ? ModelMetadataLocalizationHelper.GetTranslation(containerType, propertyName) : ModelMetadataLocalizationHelper.GetTranslation(data.DisplayName); // TODO: extract this as decorator if (data.IsRequired && ConfigurationContext.Current.ModelMetadataProviders.MarkRequiredFields && ConfigurationContext.Current.ModelMetadataProviders.RequiredFieldResource != null) { data.DisplayName += LocalizationProvider.Current.GetStringByCulture( ConfigurationContext.Current.ModelMetadataProviders.RequiredFieldResource, CultureInfo.CurrentUICulture); } var displayAttribute = theAttributes.OfType <DisplayAttribute>().FirstOrDefault(); if (displayAttribute?.Description != null) { data.Description = ModelMetadataLocalizationHelper.GetTranslation(containerType, $"{propertyName}-Description"); } return(data); }
protected override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable <Attribute> attributes) { if (metadata.ContainerType == null) { return(base.GetValidators(metadata, context, attributes)); } if (metadata.ContainerType.GetCustomAttribute <LocalizedModelAttribute>() == null) { return(base.GetValidators(metadata, context, attributes)); } foreach (var attribute in attributes.OfType <ValidationAttribute>()) { var resourceKey = ModelMetadataLocalizationHelper.BuildResourceKey($"{metadata.ContainerType.FullName}.{metadata.PropertyName}", attribute); var translation = ModelMetadataLocalizationHelper.GetValue(resourceKey); if (!string.IsNullOrEmpty(translation)) { attribute.ErrorMessage = translation; } } return(base.GetValidators(metadata, context, attributes)); }