/// <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.Value.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();

                    DisplayAttributeTransformer.Value.Transform(displayAttribute, containerType, propertyName, defaultResourceType);
                    newAttributes.Add(displayAttribute);
                }
            }

            var metadata = base.CreateMetadata(newAttributes ?? attributes, containerType, modelAccessor, modelType, propertyName);

            DisplayNameTransformer.Value.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);

                var transformer = ConventionalDataAnnotationsModelMetadataProvider.ValidationAttributeTransformer.Value;
                transformer.Transform(validationAttribute, conventionType, propertyName, defaultResourceType);
            }
        }
Exemple #3
0
        /// <summary>
        /// Tranform <see cref="ModelMetadata"/>
        /// </summary>
        /// <param name="metadata"></param>
        public static void Transform([NotNull] ModelMetadata metadata)
        {
            Invariant.IsNotNull(metadata, "metadata");

            var containerType = metadata.ContainerType;

            if (!LocalizationConventions.Enabled || containerType == null || string.IsNullOrEmpty(metadata.PropertyName))
            {
                return;
            }

            // flent configuration does not have ResourceType, so get it from type
            var resourceType = LocalizationConventions.GetDefaultResourceType(containerType);
            var propertyName = metadata.PropertyName;

            if (resourceType != null && !string.IsNullOrEmpty(propertyName))
            {
                var key = ResourceUtil.GetResourceKey(containerType, propertyName);
                if (metadata.DisplayName == null)
                {
                    metadata.DisplayName = RetrieveValue(resourceType, key, propertyName);
                }

                if (metadata.ShortDisplayName == null)
                {
                    metadata.ShortDisplayName = RetrieveValue(resourceType, key + ShortDisplayNameSuffix, propertyName + ShortDisplayNameSuffix);
                }

                if (metadata.Watermark == null)
                {
                    metadata.Watermark = RetrieveValue(resourceType, key + PromptSuffix, propertyName + PromptSuffix);
                }

                if (metadata.Description == null)
                {
                    metadata.Description = RetrieveValue(resourceType, key + DescriptionSuffix, propertyName + DescriptionSuffix);
                }
            }
        }