Esempio n. 1
0
        public static ComponentModelBase GetComponentByPropertyConfig(ModelPropertyConfig propertyConfig, bool displayMode)
        {
            if (displayMode)
            {
                return(GetComponentWithStandardSettings <DisplayTextModel>(propertyConfig));
            }

            switch (propertyConfig.DataType)
            {
            case GeneralDataType.Boolean:
            {
                return(GetComponentWithStandardSettings <CheckboxModel>(propertyConfig));
            }

            case GeneralDataType.DateTime:
            case GeneralDataType.Date:
            {
                return(GetComponentWithStandardSettings <DateTimePickerModel>(propertyConfig));
            }

            case GeneralDataType.EntityReference:
            {
                var autocomplete = GetComponentWithStandardSettings <AutocompleteModel>(propertyConfig);
                autocomplete.DataSourceType       = AutocompleteDataSourceType.EntitiesList;
                autocomplete.EntityTypeShortAlias = propertyConfig.EntityTypeShortAlias;
                return(autocomplete);
            }

            case GeneralDataType.ReferenceList:
            {
                var autocomplete = GetComponentWithStandardSettings <SelectModel>(propertyConfig);
                autocomplete.DataSourceType         = SelectDataSourceType.ReferenceList;
                autocomplete.ReferenceListName      = propertyConfig.ReferenceListName;
                autocomplete.ReferenceListNamespace = propertyConfig.ReferenceListNamespace;
                return(autocomplete);
            }

            case GeneralDataType.Numeric:
            {
                var numeric = GetComponentWithStandardSettings <NumberFieldModel>(propertyConfig);
                return(numeric);
            }

            case GeneralDataType.Text:
            {
                var textField = GetComponentWithStandardSettings <TextFieldModel>(propertyConfig);
                return(textField);
            }
            }

            return(null);
        }
Esempio n. 2
0
        private static T GetComponentWithStandardSettings <T>(ModelPropertyConfig config) where T : ComponentModelBase, new()
        {
            var type = Components.Values.FirstOrDefault(c => c.ModelType == typeof(T))?.Type;

            return(new T
            {
                Type = type,
                ApiKey = config.Path,
                Label = config.Label,
                Description = config.Description,
                Disabled = config.Readonly,
                Required = config.Required,
                SortIndex = config.OrderIndex
            });
        }
        /// <summary>
        /// Validates single property
        /// </summary>
        /// <typeparam name="T">Type of model</typeparam>
        /// <param name="model">Model instance</param>
        /// <param name="propConfig">property config</param>
        /// <param name="errors">list of errors</param>
        private static void ValidateProperty <T>(T model, ModelPropertyConfig propConfig, List <ValidationError> errors)
        {
            var prop = ReflectionHelper.GetProperty(typeof(T), propConfig.Path);

            if (prop != null)
            {
                var propValue = prop.GetValue(model);
                if (propConfig.Required && (propValue == null || (propValue is string s) && string.IsNullOrWhiteSpace(s)))
                {
                    errors.Add(new ValidationError(propConfig.Path, $"{propConfig.Label} is mandatory"));
                }

                if (propConfig.IsEmail && !string.IsNullOrWhiteSpace(propValue?.ToString()) && !propValue.ToString().IsValidEmail())
                {
                    errors.Add(new ValidationError(propConfig.Path, $"{propConfig.Label} is not valid email address"));
                }

                if (propConfig.Min.HasValue && !ValidateMin(propValue, propConfig.Min.Value))
                {
                    errors.Add(new ValidationError(propConfig.Path, $"The field {propConfig.Label} must be greater than or equal to {propConfig.Min.Value}"));
                }

                if (propConfig.Max.HasValue && !ValidateMax(propValue, propConfig.Max.Value))
                {
                    errors.Add(new ValidationError(propConfig.Path, $"The field {propConfig.Label} must be less than or equal to {propConfig.Max.Value}"));
                }

                if (propConfig.MinLength.HasValue && !ValidateMinLength(propValue, propConfig.MinLength.Value))
                {
                    errors.Add(new ValidationError(propConfig.Path, $"The field {propConfig.Label} must be a string with a minimum length of '{propConfig.MinLength}'"));
                }

                if (propConfig.MaxLength.HasValue && !ValidateMaxLength(propValue, propConfig.MaxLength.Value))
                {
                    errors.Add(new ValidationError(propConfig.Path, $"The field {propConfig.Label} must be a string with a maximum length of '{propConfig.MaxLength}'"));
                }
            }
        }
        public static ModelPropertyConfig GetHardCodedConfig(this PropertyInfo property, Type declaredType = null, bool useCamelCase = true)
        {
            var tryDeclaredType  = declaredType != null && property.DeclaringType != declaredType; // here may be different classes for example when PropertyGrid is rendered using interface
            var declaredProperty = tryDeclaredType
                                    ? declaredType.GetProperty(property.Name)
                                    : null;

            var path = useCamelCase
                ? property.Name.ToCamelCase()
                : property.Name;

            var entityConfig = property.DeclaringType != null && typeof(IEntity).IsAssignableFrom(property.DeclaringType)
                ? property.DeclaringType.GetEntityConfiguration()
                : null;
            var epc = entityConfig?[property.Name];

            var result = new ModelPropertyConfig
            {
                Path                 = path,
                Label                = ReflectionHelper.GetDisplayName(declaredProperty ?? property),
                Description          = ReflectionHelper.GetDescription(declaredProperty ?? property),
                IsVisible            = property.GetAttribute <BrowsableAttribute>()?.Browsable ?? true,
                Required             = property.HasAttribute <RequiredAttribute>(),
                Readonly             = property.GetAttribute <ReadOnlyAttribute>()?.IsReadOnly ?? false,
                DataType             = epc?.GeneralType ?? GetGeneralDataType(property.DeclaringType, property.Name),
                EntityTypeShortAlias = typeof(IEntity).IsAssignableFrom(property.PropertyType)
                    ? property.PropertyType.GetEntityConfiguration()?.SafeTypeShortAlias
                    : null,
                ReferenceListName      = epc?.ReferenceListName,
                ReferenceListNamespace = epc?.ReferenceListNamespace,
                EnumType           = epc?.EnumType,
                OrderIndex         = property.GetAttribute <DisplayAttribute>()?.GetOrder() ?? -1,
                ConfigurableByUser = property.GetAttribute <BindableAttribute>()?.Bindable ?? true,
                //GroupName = ReflectionHelper.get(declaredProperty ?? property),
            };

            return(result);
        }
 public IHtmlContent RenderEdit(HtmlHelper htmlHelper, ModelPropertyConfig property)
 {
     return(htmlHelper.Editor(property.Path, null, null, Model));
 }
 public IHtmlContent RenderDisplay(HtmlHelper htmlHelper, ModelPropertyConfig property)
 {
     return(htmlHelper.Display(property.Path, null, null, Model));
 }