Пример #1
0
 public ViewModelBase()
 {
     this.validator = new ObjectValidator(this, null, ConfigurationKey);
     this.ErrorList = new List<KeyValuePair<string, string>>();
 }
Пример #2
0
        public bool Validate()
        {
            IDictionary <string, ModelPropertyConfiguration> modelPropConfig = ControlLibraryConfig.ControlConfigReader.GetModelConfigurationSettings(TypeName, this.ConfigKey);

            if (modelPropConfig != null)
            {
                foreach (KeyValuePair <string, ModelPropertyConfiguration> keyValue in modelPropConfig)
                {
                    this.PropertyName = keyValue.Key;

                    if (keyValue.Value.IsComplexType)
                    {
                        if (keyValue.Value.IsEnumerable)
                        {
                            IEnumerable enumerable = GetNestedPropertyValue(this.data, keyValue.Key) as IEnumerable;
                            if (enumerable != null)
                            {
                                IEnumerator enumerator = enumerable.GetEnumerator();

                                int currentIndex = 0;

                                while (enumerator.MoveNext())
                                {
                                    if (enumerator.Current != null)
                                    {
                                        this.PropertyName = string.Format("{0}_{1}_", keyValue.Key, currentIndex);
                                        ObjectValidator validator = new ObjectValidator(enumerator.Current, this, ConfigKey);
                                        if (false == validator.Validate())
                                        {
                                            this.ErrorList.AddRange(validator.ErrorList);
                                        }
                                    }
                                    currentIndex++;
                                }
                            }
                        }
                        else
                        {
                            object data = GetNestedPropertyValue(this.data, keyValue.Key);
                            if (null != data)
                            {
                                ObjectValidator validator = new ObjectValidator(data, this, ConfigKey);
                                if (false == validator.Validate())
                                {
                                    this.ErrorList.AddRange(validator.ErrorList);
                                }
                            }
                        }
                    }
                    else
                    {
                        //PropertyInfo info = this.GetType().GetProperty(keyValue.Key);
                        object value = GetNestedPropertyValue(this.data, keyValue.Key);
                        IModelPropertyConfiguration imodel = modelPropConfig[keyValue.Key];
                        if (imodel != null && imodel.PropertyConfiguration != null && imodel.PropertyConfiguration.Validators != null && imodel.PropertyConfiguration.Validators.Count > 0)
                        {
                            foreach (IValidator item in imodel.PropertyConfiguration.Validators)
                            {
                                if (item.Type == ValidatorsType.Custom)
                                {
                                    CustomValidator validation = item as CustomValidator;
                                    CustomValidationExpressionConfiguration expressionConfig = ControlLibraryConfig.ControlConfigReader.GetCustomValidationExpressionConfiguration(validation.ValidationType.ToString());
                                    string _exression = string.Empty;
                                    if (expressionConfig != null)
                                    {
                                        _exression = expressionConfig.Expression;
                                    }
                                    if (item.Validate(value, _exression) == false)
                                    {
                                        if (ErrorList == null)
                                        {
                                            ErrorList = new List <KeyValuePair <string, string> >();
                                        }
                                        ErrorList.Add(new KeyValuePair <string, string>(this.GetCurrentPropertyName(), item.Message));
                                    }
                                }
                                else if (item.Type == ValidatorsType.RegExp)
                                {
                                    RegExValidator validation = item as RegExValidator;
                                    string         _exression = validation.RegExpression;

                                    if (item.Validate(value, _exression) == false)
                                    {
                                        if (ErrorList == null)
                                        {
                                            ErrorList = new List <KeyValuePair <string, string> >();
                                        }
                                        ErrorList.Add(new KeyValuePair <string, string>(this.GetCurrentPropertyName(), item.Message));
                                    }
                                }
                                else if (item.Type == ValidatorsType.Required)
                                {
                                    bool mandatory = true;
                                    if (imodel.PropertyConfiguration.SiteConfig != null && imodel.PropertyConfiguration.SiteConfig.Count > 0)
                                    {
                                        bool?configVlaueVisible = this.GetSiteConfigValue(imodel.PropertyConfiguration.SiteConfig, SiteConfigType.Visible);
                                        if (configVlaueVisible.HasValue && configVlaueVisible == false)
                                        {
                                            mandatory = false;
                                        }

                                        if (mandatory)
                                        {
                                            bool?configVlaueMandatory = this.GetSiteConfigValue(imodel.PropertyConfiguration.SiteConfig, SiteConfigType.Mandatory);
                                            if (configVlaueMandatory.HasValue && configVlaueMandatory == false)
                                            {
                                                mandatory = false;
                                            }
                                        }
                                    }
                                    if (mandatory)
                                    {
                                        if (item.Validate(value, string.Empty) == false)
                                        {
                                            if (ErrorList == null)
                                            {
                                                ErrorList = new List <KeyValuePair <string, string> >();
                                            }
                                            ErrorList.Add(new KeyValuePair <string, string>(this.GetCurrentPropertyName(), item.Message));
                                        }
                                    }
                                }
                                else
                                {
                                    if (item.Validate(value, string.Empty) == false)
                                    {
                                        if (ErrorList == null)
                                        {
                                            ErrorList = new List <KeyValuePair <string, string> >();
                                        }
                                        ErrorList.Add(new KeyValuePair <string, string>(this.GetCurrentPropertyName(), item.Message));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(this.ErrorList == null ? true : this.ErrorList.Count == 0);
        }
Пример #3
0
        public bool Validate()
        {
            IDictionary<string, ModelPropertyConfiguration> modelPropConfig = ControlLibraryConfig.ControlConfigReader.GetModelConfigurationSettings(TypeName, this.ConfigKey);

            if (modelPropConfig != null)
            {
                foreach (KeyValuePair<string, ModelPropertyConfiguration> keyValue in modelPropConfig)
                {
                    this.PropertyName = keyValue.Key;

                    if (keyValue.Value.IsComplexType)
                    {
                        
                        if (keyValue.Value.IsEnumerable)
                        {
                            IEnumerable enumerable = GetNestedPropertyValue(this.data, keyValue.Key) as IEnumerable;
                            if (enumerable != null)
                            {
                                IEnumerator enumerator = enumerable.GetEnumerator();

                                int currentIndex = 0;

                                while (enumerator.MoveNext())
                                {
                                    if (enumerator.Current != null)
                                    {
                                        this.PropertyName = string.Format("{0}_{1}_",keyValue.Key,currentIndex);
                                        ObjectValidator validator = new ObjectValidator(enumerator.Current, this, ConfigKey);
                                        if (false == validator.Validate())
                                        {
                                            this.ErrorList.AddRange(validator.ErrorList);
                                        }
                                    }
                                    currentIndex++;
                                }
                            }
                        }
                        else
                        {
                            object data = GetNestedPropertyValue(this.data, keyValue.Key);
                            if (null != data)
                            {
                                ObjectValidator validator = new ObjectValidator(data, this, ConfigKey);
                                if (false == validator.Validate())
                                {
                                    this.ErrorList.AddRange(validator.ErrorList);
                                }
                            }
                        }
                    }
                    else
                    {
                        //PropertyInfo info = this.GetType().GetProperty(keyValue.Key);
                        object value = GetNestedPropertyValue(this.data, keyValue.Key);
                        IModelPropertyConfiguration imodel = modelPropConfig[keyValue.Key];
                        if (imodel != null && imodel.PropertyConfiguration != null && imodel.PropertyConfiguration.Validators != null && imodel.PropertyConfiguration.Validators.Count > 0)
                        {
                            foreach (IValidator item in imodel.PropertyConfiguration.Validators)
                            {
                                if (item.Type == ValidatorsType.Custom)
                                {
                                    CustomValidator validation = item as CustomValidator;
                                    CustomValidationExpressionConfiguration expressionConfig = ControlLibraryConfig.ControlConfigReader.GetCustomValidationExpressionConfiguration(validation.ValidationType.ToString());
                                    string _exression = string.Empty;
                                    if (expressionConfig != null)
                                    {
                                        _exression = expressionConfig.Expression;
                                    }
                                    if (item.Validate(value, _exression) == false)
                                    {
                                        if (ErrorList == null)
                                        {
                                            ErrorList = new List<KeyValuePair<string, string>>();
                                        }
                                        ErrorList.Add(new KeyValuePair<string, string>(this.GetCurrentPropertyName(), item.Message));
                                    }
                                }
                                else if (item.Type == ValidatorsType.RegExp)
                                {
                                    RegExValidator validation = item as RegExValidator;
                                    string _exression = validation.RegExpression;

                                    if (item.Validate(value, _exression) == false)
                                    {
                                        if (ErrorList == null)
                                        {
                                            ErrorList = new List<KeyValuePair<string, string>>();
                                        }
                                        ErrorList.Add(new KeyValuePair<string, string>(this.GetCurrentPropertyName(), item.Message));
                                    }
                                }
                                else if (item.Type == ValidatorsType.Required)
                                {
                                    bool mandatory = true;
                                    if (imodel.PropertyConfiguration.SiteConfig != null && imodel.PropertyConfiguration.SiteConfig.Count > 0)
                                    {
                                        bool? configVlaueVisible = this.GetSiteConfigValue(imodel.PropertyConfiguration.SiteConfig, SiteConfigType.Visible);
                                        if (configVlaueVisible.HasValue && configVlaueVisible == false)
                                        {
                                            mandatory = false;
                                        }

                                        if (mandatory)
                                        {
                                            bool? configVlaueMandatory = this.GetSiteConfigValue(imodel.PropertyConfiguration.SiteConfig, SiteConfigType.Mandatory);
                                            if (configVlaueMandatory.HasValue && configVlaueMandatory == false)
                                            {
                                                mandatory = false;
                                            }
                                        }
                                    }
                                    if (mandatory)
                                    {
                                        if (item.Validate(value, string.Empty) == false)
                                        {
                                            if (ErrorList == null)
                                            {
                                                ErrorList = new List<KeyValuePair<string, string>>();
                                            }
                                            ErrorList.Add(new KeyValuePair<string, string>(this.GetCurrentPropertyName(), item.Message));
                                        } 
                                    }
                                }
                                else
                                {
                                    if (item.Validate(value, string.Empty) == false)
                                    {
                                        if (ErrorList == null)
                                        {
                                            ErrorList = new List<KeyValuePair<string, string>>();
                                        }
                                        ErrorList.Add(new KeyValuePair<string, string>(this.GetCurrentPropertyName(), item.Message));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return this.ErrorList == null ? true : this.ErrorList.Count == 0;
        }
Пример #4
0
 public ViewModelBase()
 {
     this.validator = new ObjectValidator(this, null, ConfigurationKey);
     this.ErrorList = new List <KeyValuePair <string, string> >();
 }