/// <summary> /// Configures the <see cref="ModelMetadataItem"/> /// </summary> /// <param name="item"></param> void IModelMetadataItemConfigurator.Configure(ModelMetadataItem item) { foreach (var action in _actions) { action(item); } }
/// <summary> /// Creates a set of model metadata rules /// </summary> /// <param name="property">Target property information</param> /// <param name="item"></param> /// <returns>A instance of <see cref="ModelMetadataItem"/></returns> public virtual ModelMetadataItem Apply(PropertyInfo property, ModelMetadataItem item) { var builder = new ModelMetadataItemBuilder <T>(item); Apply(property, builder); return(builder.Item); }
/// <summary> /// Creates a set of model metadata rules /// </summary> /// <param name="property">Target property information</param> /// <param name="item"></param> /// <returns>A instance of <see cref="ModelMetadataItem"/></returns> public virtual void Apply(PropertyInfo property, ModelMetadataItem item) { var builder = new ModelMetadataItemBuilder <T>(item); Apply(builder); Configure(builder, item); }
/// <summary> /// Registers the model type metadata. /// </summary> /// <param name="modelType">Type of the model.</param> /// <param name="metadataItem">The metadata.</param> public virtual void RegisterModel(Type modelType, ModelMetadataItem metadataItem) { Invariant.IsNotNull(modelType, "modelType"); Invariant.IsNotNull(metadataItem, "metadataItem"); ModelMetadataRegistryItem item = GetOrCreate(modelType); item.ClassMetadata = metadataItem; }
private ModelMetadataItem Append(string property) { Invariant.IsNotNull(property, "property"); var item = new ModelMetadataItem(); configurations[property] = item; return(item); }
protected ModelMetadataItemBuilder <object> Configure([NotNull] string property) { Invariant.IsNotNull(property, "property"); var item = new ModelMetadataItem(); var builder = new ModelMetadataItemBuilder <object>(item); configurations[property] = builder; return(builder); }
public static ModelMetadataItem CreateModelMetadataItem(ModelMetadata modelMetadata, IEnumerable<Attribute> attributes) { Throw.IfNullArgument(modelMetadata, "modelMetadata"); var item = new ModelMetadataItem(); item.ShowForDisplay = modelMetadata.ShowForDisplay; item.IsReadOnly = modelMetadata.IsReadOnly; item.ShowForEdit = modelMetadata.ShowForEdit && !modelMetadata.IsReadOnly; DisplayAttribute displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault(); RequiredAttribute requiredAttribute = attributes.OfType<RequiredAttribute>().FirstOrDefault(); DisplayFormatAttribute displayFormatAttribute = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault(); if (displayAttribute != null) item.DisplayName = () => displayAttribute.GetName(); else item.DisplayName = () => modelMetadata.GetDisplayName(); if (displayAttribute != null) item.ShortDisplayName = () => displayAttribute.GetShortName(); else item.ShortDisplayName = () => modelMetadata.ShortDisplayName; item.TemplateName = modelMetadata.TemplateHint ?? modelMetadata.DataTypeName; if (displayAttribute != null) item.Description = () => displayAttribute.GetDescription(); else item.Description = () => modelMetadata.Description; if (modelMetadata.NullDisplayText != null) item.NullDisplayText = () => modelMetadata.NullDisplayText; if (modelMetadata.Watermark != null) item.Watermark = () => modelMetadata.Watermark; item.HideSurroundingHtml = modelMetadata.HideSurroundingHtml; item.RequestValidationEnabled = modelMetadata.RequestValidationEnabled; item.IsRequired = modelMetadata.IsRequired; item.Order = modelMetadata.Order; item.ConvertEmptyStringToNull = modelMetadata.ConvertEmptyStringToNull; if (displayFormatAttribute != null) item.DisplayFormat = () => displayFormatAttribute.DataFormatString; else if (modelMetadata.DisplayFormatString != null) item.DisplayFormat = () => modelMetadata.DisplayFormatString; if (displayFormatAttribute != null && displayFormatAttribute.ApplyFormatInEditMode) item.EditFormat = () => modelMetadata.DisplayFormatString; else if (modelMetadata.EditFormatString != null) item.EditFormat = () => modelMetadata.EditFormatString; item.ApplyFormatInEditMode = item.EditFormat != null; return item; }
private ModelMetadata CreatePropertyMetadata( Type containerType, string propertyName, Type propertyType, ModelMetadataItem propertyMetadata, Func <object> modelAccessor) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, containerType, modelAccessor, propertyType, propertyName, propertyMetadata); if (propertyMetadata != null) { Copy(propertyMetadata, modelMetadata); } return(modelMetadata); }
public ModelMetadataItem GetPropertyMetadataOrCreateNew(string name) { ModelMetadataItem propertyMetadata; if (!PropertiesMetadata.TryGetValue(name, out propertyMetadata)) { propertyMetadata = new ModelMetadataItem(); PropertiesMetadata.Add(name, propertyMetadata); } return(propertyMetadata); }
/// <summary> /// Returns model metadata additional setting of type <typeparamref name="TSetting"/> associated with this <paramref name="item"/>. /// New model setting will be created if no one is found. /// </summary> /// <param name="item"></param> /// <typeparam name="TSetting"></typeparam> /// <returns>Model validation metadata of type <typeparamref name="TSetting"/></returns> public static TSetting GetAdditionalSettingOrCreateNew <TSetting>(this ModelMetadataItem item) where TSetting : class, IModelMetadataAdditionalSetting, new() { TSetting setting = item.GetAdditionalSetting <TSetting>(); if (setting == null) { setting = new TSetting(); item.AdditionalSettings.Add(setting); } return(setting); }
public static TSetting GetMetadataAwareOrCreateNew <TSetting>([NotNull] this ModelMetadataItem item) where TSetting : class, IMetadataAware, new() { var setting = item.GetMetadataAware <TSetting>(); if (setting == null) { setting = new TSetting(); item.MetadataAwares.Add(setting); } return(setting); }
private static void SyncValidations([NotNull] ModelMetadataItem metadataFrom, [NotNull] ModelMetadataItem metadataTo) { if (metadataFrom.Validations.Count > 0) { var types = new HashSet <Type>(); foreach (var metadata in metadataTo.Validations) { var type = metadata.GetType(); if (!types.Contains(type)) { types.Add(type); } } //TODO: how about Deleged validation? just rewrite it completely? foreach (var validation in metadataFrom.Validations) { if (!types.Contains(validation.GetType())) { metadataTo.Validations.Add(validation); } } } if (metadataTo.IsRequired == null && metadataFrom.IsRequired.HasValue && metadataFrom.IsRequired.Value) { metadataTo.IsRequired = metadataFrom.IsRequired.Value; } // ensure that Required attribute is removed if (metadataFrom.IsRequired.HasValue && !metadataFrom.IsRequired.Value) { var requiredValidation = metadataTo.GetValidation <RequiredValidationMetadata>(); if (requiredValidation != null) { metadataTo.Validations.Remove(requiredValidation); } metadataTo.IsRequired = false; } }
private static void SyncAdditionalSettings([NotNull] ModelMetadataItem metadataFrom, [NotNull] ModelMetadataItem metadataTo) { if (metadataFrom.AdditionalSettings.Count > 0) { var additionalSettingsTypes = new HashSet <Type>(); foreach (var setting in metadataTo.AdditionalSettings) { var type = setting.GetType(); if (!additionalSettingsTypes.Contains(type)) { additionalSettingsTypes.Add(type); } } foreach (var setting in metadataFrom.AdditionalSettings) { if (!additionalSettingsTypes.Contains(setting.GetType())) { metadataTo.AdditionalSettings.Add(setting); } } } }
/// <summary> /// Gets metadata for the specified property. /// </summary> /// <param name="modelAccessor">The model accessor.</param> /// <param name="containerType">The type of the container.</param> /// <param name="propertyName">The property to get the metadata model for.</param> /// <returns> /// The metadata model for the specified property. /// </returns> public override ModelMetadata GetMetadataForProperty(Func <object> modelAccessor, Type containerType, string propertyName) { Invariant.IsNotNull(containerType, "containerType"); Invariant.IsNotNull(propertyName, "propertyName"); ModelMetadataItem metadataItem = registry.GetModelPropertyMetadata(containerType, propertyName); if (metadataItem == null) { return(base.GetMetadataForProperty(modelAccessor, containerType, propertyName)); } PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(containerType) .Cast <PropertyDescriptor>() .FirstOrDefault(property => property.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); if (propertyDescriptor == null) { throw new ArgumentException(string.Format(Culture.CurrentUI, ExceptionMessages.ThePropertyNameOfTypeCouldNotBeFound, containerType.FullName, propertyName)); } return(CreatePropertyMetadata(containerType, propertyName, propertyDescriptor.PropertyType, metadataItem, modelAccessor)); }
private void ApplyMetadataConvenstions([NotNull] Type modelType, [NotNull] ModelMetadataRegistryItem item) { var properties = modelType.GetProperties(); foreach (var convention in conventions) { var metadataConvention = convention; foreach (var pi in properties.Where(metadataConvention.IsApplicable)) { ModelMetadataItem metadataItem; if (!item.PropertiesMetadata.TryGetValue(pi.Name, out metadataItem)) { metadataItem = new ModelMetadataItem(); item.PropertiesMetadata.Add(pi.Name, metadataItem); } var conventionalItem = new ModelMetadataItem(); convention.Apply(pi, conventionalItem); conventionalItem.MergeTo(metadataItem); } } item.IsConventionsApplied = true; }
public static TSetting GetMetadataAware <TSetting>([NotNull] this ModelMetadataItem item) where TSetting : IMetadataAware { return(item.MetadataAwares.OfType <TSetting>().FirstOrDefault()); }
/// <summary> /// Initializes a new instance of the <see cref="ExtendedModelMetadata"/> class. /// </summary> /// <param name="provider">The provider.</param> /// <param name="containerType">Type of the container.</param> /// <param name="modelAccessor">The model accessor.</param> /// <param name="modelType">Type of the model.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="metadata">The metadata.</param> public ExtendedModelMetadata(ModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName, ModelMetadataItem metadata) : base(provider, containerType, modelAccessor, modelType, propertyName) { Metadata = metadata; }
/// <summary> /// Initializes a new instance of the <see cref="ModelMetadataItemBuilder{TValue}"/> class. /// </summary> /// <param name="item">The item.</param> public ModelMetadataItemBuilder(ModelMetadataItem item) { Invariant.IsNotNull(item, "item"); Item = item; }
private static void Copy(ModelMetadataItem metadataItem, ModelMetadata metadata) { metadata.ShowForDisplay = metadataItem.ShowForDisplay; if (metadataItem.DisplayName != null) { metadata.DisplayName = metadataItem.DisplayName(); } if (metadataItem.ShortDisplayName != null) { metadata.ShortDisplayName = metadataItem.ShortDisplayName(); } if (!string.IsNullOrEmpty(metadataItem.TemplateName)) { metadata.TemplateHint = metadataItem.TemplateName; } if (metadataItem.Description != null) { metadata.Description = metadataItem.Description(); } if (metadataItem.NullDisplayText != null) { metadata.NullDisplayText = metadataItem.NullDisplayText(); } if (metadataItem.Watermark != null) { metadata.Watermark = metadataItem.Watermark(); } if (metadataItem.HideSurroundingHtml.HasValue) { metadata.HideSurroundingHtml = metadataItem.HideSurroundingHtml.Value; } if (metadataItem.RequestValidationEnabled.HasValue) { metadata.RequestValidationEnabled = metadataItem.RequestValidationEnabled.Value; } if (metadataItem.IsReadOnly.HasValue) { metadata.IsReadOnly = metadataItem.IsReadOnly.Value; } if (metadataItem.IsRequired.HasValue) { metadata.IsRequired = metadataItem.IsRequired.Value; } if (metadataItem.ShowForEdit.HasValue) { metadata.ShowForEdit = metadataItem.ShowForEdit.Value; } else { metadata.ShowForEdit = !metadata.IsReadOnly; } IModelMetadataFormattableItem formattableItem = metadataItem as IModelMetadataFormattableItem; if (formattableItem != null) { if (formattableItem.DisplayFormat != null) { metadata.DisplayFormatString = formattableItem.DisplayFormat(); } if (formattableItem.ApplyFormatInEditMode && metadata.ShowForEdit && formattableItem.EditFormat != null) { metadata.EditFormatString = formattableItem.EditFormat(); } } StringMetadataItem stringMetadataItem = metadataItem as StringMetadataItem; if (stringMetadataItem != null) { metadata.ConvertEmptyStringToNull = stringMetadataItem.ConvertEmptyStringToNull; } }
/// <summary> /// Returns model metadata additional setting of type <typeparamref name="TSetting"/> associated with this <paramref name="item"/> or null. /// </summary> /// <param name="item"></param> /// <typeparam name="TSetting"></typeparam> /// <returns>Model validation metadata of type <typeparamref name="TSetting"/> or null</returns> public static TSetting GetAdditionalSetting <TSetting>(this ModelMetadataItem item) where TSetting : IModelMetadataAdditionalSetting { return(item.AdditionalSettings.OfType <TSetting>().FirstOrDefault()); }
private static void SetMetadataProperties(ModelMetadataItem metadataFrom, ModelMetadataItem metadataTo) { // by default the value is true, so will not set it to true if original value was set to false if (metadataFrom.ShowForDisplay == false) { metadataTo.ShowForDisplay = metadataFrom.ShowForDisplay; } // by default the value is false, so will not set it to false if original value was set to true if (metadataFrom.ApplyFormatInEditMode) { metadataTo.ApplyFormatInEditMode = metadataFrom.ApplyFormatInEditMode; } if (metadataTo.DisplayName == null && metadataFrom.DisplayName != null) { metadataTo.DisplayName = metadataFrom.DisplayName; } if (metadataTo.ShortDisplayName == null && metadataFrom.ShortDisplayName != null) { metadataTo.ShortDisplayName = metadataFrom.ShortDisplayName; } if (string.IsNullOrEmpty(metadataTo.TemplateName) && !string.IsNullOrEmpty(metadataFrom.TemplateName)) { metadataTo.TemplateName = metadataFrom.TemplateName; } if (metadataTo.Description == null && metadataFrom.Description != null) { metadataTo.Description = metadataFrom.Description; } if (metadataTo.NullDisplayText == null && metadataFrom.NullDisplayText != null) { metadataTo.NullDisplayText = metadataFrom.NullDisplayText; } if (metadataTo.Watermark == null && metadataFrom.Watermark != null) { metadataTo.Watermark = metadataFrom.Watermark; } if (metadataTo.HideSurroundingHtml == null && metadataFrom.HideSurroundingHtml.HasValue) { metadataTo.HideSurroundingHtml = metadataFrom.HideSurroundingHtml.Value; } if (metadataTo.RequestValidationEnabled == null && metadataFrom.RequestValidationEnabled.HasValue) { metadataTo.RequestValidationEnabled = metadataFrom.RequestValidationEnabled.Value; } if (metadataTo.IsReadOnly == null && metadataFrom.IsReadOnly.HasValue) { metadataTo.IsReadOnly = metadataFrom.IsReadOnly.Value; } if (metadataTo.ShowForEdit == null && metadataFrom.ShowForEdit.HasValue) { metadataTo.ShowForEdit = metadataFrom.ShowForEdit.Value; } if (metadataTo.EditFormat == null && metadataFrom.EditFormat != null) { metadataTo.EditFormat = metadataFrom.EditFormat; } if (metadataTo.Order == null && metadataFrom.Order.HasValue) { metadataTo.Order = metadataFrom.Order; } if (metadataTo.DisplayFormat == null && metadataFrom.DisplayFormat != null) { metadataTo.DisplayFormat = metadataFrom.DisplayFormat; } if (metadataTo.ConvertEmptyStringToNull == null && metadataFrom.ConvertEmptyStringToNull.HasValue) { metadataTo.ConvertEmptyStringToNull = metadataFrom.ConvertEmptyStringToNull; } }
private ModelMetadata CreateModelMetadata(Type modelType, Func<object> modelAccessor, ModelMetadataItem metadataItem) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, null, modelAccessor, modelType, null, metadataItem); if (metadataItem != null) { Copy(metadataItem, modelMetadata); } return modelMetadata; }
/// <summary> /// Initializes a new instance of the <see cref="ExtendedModelMetadata"/> class. /// </summary> /// <param name="provider">The provider.</param> /// <param name="containerType">Type of the container.</param> /// <param name="modelAccessor">The model accessor.</param> /// <param name="modelType">Type of the model.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="metadata">The metadata.</param> public ExtendedModelMetadata(ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName, ModelMetadataItem metadata) : base(provider, containerType, modelAccessor, modelType, propertyName) { Metadata = metadata; }
/// <summary> /// Returns model validation metadata of type <typeparamref name="TValidationMetadata"/> associated with this <paramref name="item"/>. /// New validation will be created if no one is found. /// </summary> /// <param name="item"></param> /// <typeparam name="TValidationMetadata"></typeparam> /// <returns>Model validation metadata of type <typeparamref name="TValidationMetadata"/></returns> public static TValidationMetadata GetValidationOrCreateNew <TValidationMetadata>(this ModelMetadataItem item) where TValidationMetadata : class, IModelValidationMetadata, new() { TValidationMetadata validation = item.GetValidation <TValidationMetadata>(); if (validation == null) { validation = new TValidationMetadata(); item.Validations.Add(validation); } return(validation); }
/// <summary> /// Initializes a new instance of the <see cref="ModelMetadataItemBuilder{TValue}"/> class. /// </summary> /// <param name="item">The item.</param> public ModelMetadataItemBuilder([NotNull] ModelMetadataItem item) { Invariant.IsNotNull(item, "item"); }
/// <summary> /// Returns model validation metadata of type <typeparamref name="TValidationMetadata"/> associated with this <paramref name="item"/> or null. /// </summary> /// <param name="item"></param> /// <typeparam name="TValidationMetadata"></typeparam> /// <returns>Model validation metadata of type <typeparamref name="TValidationMetadata"/> or null</returns> public static TValidationMetadata GetValidation <TValidationMetadata>(this ModelMetadataItem item) where TValidationMetadata : IModelValidationMetadata { return(item.Validations.OfType <TValidationMetadata>().SingleOrDefault()); }
private static void Copy(ModelMetadataItem metadataItem, ModelMetadata metadata) { metadata.ShowForDisplay = metadataItem.ShowForDisplay; if (metadataItem.DisplayName != null) { metadata.DisplayName = metadataItem.DisplayName(); } if (metadataItem.ShortDisplayName != null) { metadata.ShortDisplayName = metadataItem.ShortDisplayName(); } if (!string.IsNullOrEmpty(metadataItem.TemplateName)) { metadata.TemplateHint = metadataItem.TemplateName; } if (metadataItem.Description != null) { metadata.Description = metadataItem.Description(); } if (metadataItem.NullDisplayText != null) { metadata.NullDisplayText = metadataItem.NullDisplayText(); } if (metadataItem.Watermark != null) { metadata.Watermark = metadataItem.Watermark(); } if (metadataItem.HideSurroundingHtml.HasValue) { metadata.HideSurroundingHtml = metadataItem.HideSurroundingHtml.Value; } if (metadataItem.RequestValidationEnabled.HasValue) { metadata.RequestValidationEnabled = metadataItem.RequestValidationEnabled.Value; } if (metadataItem.IsReadOnly.HasValue) { metadata.IsReadOnly = metadataItem.IsReadOnly.Value; } if (metadataItem.IsRequired.HasValue) { metadata.IsRequired = metadataItem.IsRequired.Value; } if (metadataItem.ShowForEdit.HasValue) { metadata.ShowForEdit = metadataItem.ShowForEdit.Value; } else { metadata.ShowForEdit = !metadata.IsReadOnly; } if (metadataItem.Order.HasValue) { metadata.Order = metadataItem.Order.Value; } if (metadataItem.DisplayFormat != null) { metadata.DisplayFormatString = metadataItem.DisplayFormat(); } if (metadataItem.ApplyFormatInEditMode && metadata.ShowForEdit && metadataItem.EditFormat != null) { metadata.EditFormatString = metadataItem.EditFormat(); } if (metadataItem.ConvertEmptyStringToNull.HasValue) { metadata.ConvertEmptyStringToNull = metadataItem.ConvertEmptyStringToNull.Value; } FluentModelMetadataTransformer.Transform(metadata); MvcExtensions.DisplayNameTransformer.Transform(metadata); }
private ModelMetadata CreateModelMetadata(Type modelType, Func <object> modelAccessor, ModelMetadataItem metadataItem) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, null, modelAccessor, modelType, null, metadataItem); if (metadataItem != null) { Copy(metadataItem, modelMetadata); } return(modelMetadata); }
/// <summary> /// Merge metadata items /// </summary> public static void MergeTo([NotNull] this ModelMetadataItem metadataFrom, [NotNull] ModelMetadataItem metadataTo) { SetMetadataProperties(metadataFrom, metadataTo); SyncValidations(metadataFrom, metadataTo); SyncAdditionalSettings(metadataFrom, metadataTo); }
private static void SetupDataTypeValidator(ModelMetadataItem property) { var typeValidator = property.GetValidationOrCreateNew<CustomValidationMetadata<DataTypeModelValidator>>(); typeValidator.Factory = (metadata, context) => new DataTypeModelValidator(metadata, context); typeValidator.Configure = v => DataTypeModelValidator.Configure(v); }
/// <summary> /// Gets metadata for the specified model accessor and model type. /// </summary> /// <param name="modelAccessor">The model accessor.</param> /// <param name="modelType">They type of the model.</param> /// <returns>The metadata.</returns> public override ModelMetadata GetMetadataForType(Func <object> modelAccessor, Type modelType) { ModelMetadataItem metadataItem = registry.GetModelMetadata(modelType); return(metadataItem == null?base.GetMetadataForType(modelAccessor, modelType) : CreateModelMetadata(modelType, modelAccessor, metadataItem)); }
private static void Copy(ModelMetadataItem metadataItem, ModelMetadata metadata) { metadata.ShowForDisplay = metadataItem.ShowForDisplay; if (metadataItem.DisplayName != null) { metadata.DisplayName = metadataItem.DisplayName(); } if (metadataItem.ShortDisplayName != null) { metadata.ShortDisplayName = metadataItem.ShortDisplayName(); } if (!string.IsNullOrEmpty(metadataItem.TemplateName)) { metadata.TemplateHint = metadataItem.TemplateName; } if (metadataItem.Description != null) { metadata.Description = metadataItem.Description(); } if (metadataItem.NullDisplayText != null) { metadata.NullDisplayText = metadataItem.NullDisplayText(); } if (metadataItem.Watermark != null) { metadata.Watermark = metadataItem.Watermark(); } if (metadataItem.HideSurroundingHtml.HasValue) { metadata.HideSurroundingHtml = metadataItem.HideSurroundingHtml.Value; } if (metadataItem.RequestValidationEnabled.HasValue) { metadata.RequestValidationEnabled = metadataItem.RequestValidationEnabled.Value; } if (metadataItem.IsReadOnly.HasValue) { metadata.IsReadOnly = metadataItem.IsReadOnly.Value; } if (metadataItem.IsRequired.HasValue) { metadata.IsRequired = metadataItem.IsRequired.Value; } if (metadataItem.ShowForEdit.HasValue) { metadata.ShowForEdit = metadataItem.ShowForEdit.Value; } else { metadata.ShowForEdit = !metadata.IsReadOnly; } if (metadataItem.Order.HasValue) { metadata.Order = metadataItem.Order.Value; } if (metadataItem.DisplayFormat != null) { metadata.DisplayFormatString = metadataItem.DisplayFormat(); } if (metadataItem.ApplyFormatInEditMode && metadata.ShowForEdit && metadataItem.EditFormat != null) { metadata.EditFormatString = metadataItem.EditFormat(); } if (metadataItem.ConvertEmptyStringToNull.HasValue) { metadata.ConvertEmptyStringToNull = metadataItem.ConvertEmptyStringToNull.Value; } }
static void Configure(IModelMetadataItemConfigurator builder, ModelMetadataItem item) { builder.Configure(item); }
private ModelMetadata CreatePropertyMetadata(Type containerType, string propertyName, Type propertyType, ModelMetadataItem propertyMetadata, Func<object> modelAccessor) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, containerType, modelAccessor, propertyType, propertyName, propertyMetadata); if (propertyMetadata != null) { Copy(propertyMetadata, modelMetadata); } return modelMetadata; }
private ModelValidationMetadata FindRangeMetadata(ModelMetadataItem property) { return (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<int>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<sbyte>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<short>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<long>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<uint>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<byte>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<ushort>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<ulong>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<float>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<double>>() ?? (ModelValidationMetadata)property.GetValidation<RangeValidationMetadata<decimal>>(); }
private static void SetupRequiredValidator(ModelMetadataItem property) { var requiredValidator = property.GetValidationOrCreateNew<RequiredValidationMetadata>(); // check that message is not localized yet if (requiredValidator.ErrorMessage == null && requiredValidator.ErrorMessageResourceType == null) { requiredValidator.ErrorMessage = () => ValidationResources.PropertyValueRequired; } }
private static ModelValidationMetadata CreateRangeValidator(ModelMetadataItem property, RangeAttribute rangeAttribute) { object min = rangeAttribute.Minimum; object max = rangeAttribute.Maximum; // Check all numeric types, no other way found ModelValidationMetadata extValidator = null; if (min is int) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<int>>(); else if (min is sbyte) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<sbyte>>(); else if (min is short) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<short>>(); else if (min is long) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<long>>(); else if (min is uint) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<uint>>(); else if (min is byte) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<byte>>(); else if (min is ushort) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<ushort>>(); else if (min is ulong) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<ulong>>(); else if (min is float) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<float>>(); else if (min is double) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<double>>(); else if (min is decimal) extValidator = property.GetValidationOrCreateNew<RangeValidationMetadata<decimal>>(); if (extValidator != null) { if (rangeAttribute.ErrorMessageResourceType != null) { extValidator.ErrorMessageResourceType = rangeAttribute.ErrorMessageResourceType; extValidator.ErrorMessageResourceName = rangeAttribute.ErrorMessageResourceName; } } return extValidator; }