示例#1
0
        /// <summary>
        ///
        /// </summary>
        public LengthValidation.Builder LengthValidation(LengthValidation component)
        {
#if MVC
            component.ViewContext = this.HtmlHelper != null ? this.HtmlHelper.ViewContext : null;
#endif
            return(new LengthValidation.Builder(component));
        }
示例#2
0
        public static ModelField CreateModelFieldFromMeta(ModelMetadata propertyMetadata, Ext.Net.Model storeModel = null, ModelFieldAttribute modelAttr = null, ControllerContext controllerContext = null)
        {
            var modelField = new ModelField();
            if (modelAttr == null)
            {
                modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null;
            }           

            modelField.Name = propertyMetadata.PropertyName;

            if (Store.IsDate(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Date;
            }
            else if (Store.IsInteger(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Int;
            }
            else if (Store.IsFloat(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Float;
            }
            else if (Store.IsBoolean(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Boolean;
            }
            else if (propertyMetadata.ModelType == typeof(string))
            {
                modelField.Type = ModelFieldType.String;
            }

            if (propertyMetadata.IsComplexType && modelAttr != null)
            {
                modelField.IsComplex = true;
            }

            if (modelAttr != null && storeModel != null)
            {
                modelAttr.CopyTo(modelField, storeModel);
            }

            if(storeModel != null)
            {
                storeModel.Fields.Add(modelField);
            }

            ValidationCollection validations = new ValidationCollection();
            if (propertyMetadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY))
            {
                validations = (ValidationCollection)propertyMetadata.AdditionalValues[AbstractValidationAttribute.KEY];                
            }

            foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext))
            {
                var rule = v.GetClientValidationRules().FirstOrDefault();

                if (rule != null)
                {
                    switch (rule.ValidationType)
                    {
                        case "required":
                            validations.Add(new PresenceValidation { Message = rule.ErrorMessage });
                            break;
                        case "regex":
                            if (rule.ValidationParameters.ContainsKey("pattern"))
                            {
                                validations.Add(new FormatValidation { Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage });
                            }
                            break;                        
                        case "length":
                            int num;
                            LengthValidation length = new LengthValidation();
                            length.Message = rule.ErrorMessage;
                            if (rule.ValidationParameters.ContainsKey("max"))
                            {
                                if (int.TryParse(rule.ValidationParameters["max"].ToString(), out num))
                                {
                                    length.Max = num;
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            if (rule.ValidationParameters.ContainsKey("min"))
                            {
                                if (int.TryParse(rule.ValidationParameters["min"].ToString(), out num))
                                {
                                    length.Min = num;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            validations.Add(length);
                            break;
                    }
                }
            }

            if (controllerContext != null)
            {
                foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext))
                {
                    var rule = v.GetClientValidationRules().FirstOrDefault();

                    if (rule != null)
                    {
                        switch (rule.ValidationType)
                        {
                            case "required":
                                validations.Add(new PresenceValidation { Field = modelField.Name, Message = rule.ErrorMessage });
                                break;
                            case "regex":
                                if (rule.ValidationParameters.ContainsKey("pattern"))
                                {
                                    validations.Add(new FormatValidation { Field = modelField.Name, Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage });
                                }
                                break;
                            case "length":
                                var lengthVal = new LengthValidation();
                                if (rule.ValidationParameters.ContainsKey("max"))
                                {
                                    lengthVal.Max = (int)Convert.ChangeType(rule.ValidationParameters["max"], typeof(int));
                                }

                                if (rule.ValidationParameters.ContainsKey("min"))
                                {
                                    lengthVal.Min = (int)Convert.ChangeType(rule.ValidationParameters["min"], typeof(int));
                                }

                                lengthVal.Message = rule.ErrorMessage;
                                validations.Add(lengthVal);
                                break;
                        }
                    }
                }
            }

            if (validations.Count > 0)
            {
                if (storeModel != null)
                {
                    storeModel.Validations.AddRange(validations);
                }
                else
                {
                    modelField.Validations = validations;
                }
            }

            return modelField;
        }
        protected override void OnMetadataProcess(ModelMetadata meta, string name, ViewDataDictionary viewData, ControllerContext context)
        {
            base.OnMetadataProcess(meta, name, viewData, context);

            if (meta.IsRequired)
            {
                this.AllowBlank = false;
            }

            if (this.EmptyText.IsEmpty() && meta.Watermark.IsNotEmpty())
            {
                this.EmptyText = meta.Watermark;
            }

            string type = meta.DataTypeName;

            if (type.IsNotEmpty())
            {
                switch (type)
                {
                case "Date":
                    this.InputType = Ext.Net.InputType.Date;
                    break;

                case "DateTime":
                    this.InputType = Ext.Net.InputType.DateTime;
                    break;

                case "EmailAddress":
                    this.InputType     = Ext.Net.InputType.Email;
                    this.StandardVtype = ValidationType.Email;
                    break;

                case "Password":
                    this.InputType = Ext.Net.InputType.Password;
                    break;

                case "Time":
                    this.InputType = Ext.Net.InputType.Time;
                    break;

                case "Url":
                    this.InputType     = Ext.Net.InputType.Url;
                    this.StandardVtype = ValidationType.Url;
                    break;
                }
            }

            if (meta.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY))
            {
                ValidationCollection validations = (ValidationCollection)meta.AdditionalValues[AbstractValidationAttribute.KEY];

                AbstractValidation required = validations.FirstOrDefault(v => v is PresenceValidation);

                if (required != null)
                {
                    this.AllowBlank = false;

                    if (required.Message.IsNotEmpty())
                    {
                        this.BlankText = required.Message;
                    }
                }

                AbstractValidation email = validations.FirstOrDefault(v => v is EmailValidation);

                if (email != null)
                {
                    this.StandardVtype = ValidationType.Email;

                    if (email.Message.IsNotEmpty())
                    {
                        this.VtypeText = email.Message;
                    }
                }

                LengthValidation length = validations.FirstOrDefault(v => v is LengthValidation) as LengthValidation;

                if (length != null)
                {
                    if (length.Max != int.MaxValue)
                    {
                        this.MaxLength = length.Max;
                    }

                    if (length.Min != int.MinValue)
                    {
                        this.MinLength = length.Min;
                    }

                    if (length.Message.IsNotEmpty())
                    {
                        this.MaxLengthText = length.Message;
                        this.MinLengthText = length.Message;
                    }
                }

                FormatValidation format = validations.FirstOrDefault(v => v is FormatValidation) as FormatValidation;

                if (format != null)
                {
                    this.Regex = format.Matcher;

                    if (format.Message.IsNotEmpty())
                    {
                        this.RegexText = format.Message;
                    }
                }
            }
        }
 /// <summary>
 ///
 /// </summary>
 public LengthValidation.Builder LengthValidation(LengthValidation component)
 {
     return(new LengthValidation.Builder(component));
 }
示例#5
0
        public static ModelField CreateModelFieldFromMeta(ModelMetadata propertyMetadata, Ext.Net.Model storeModel = null, ModelFieldAttribute modelAttr = null, ControllerContext controllerContext = null)
        {
            var modelField = new ModelField();

            if (modelAttr == null)
            {
                modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null;
            }

            modelField.Name = propertyMetadata.PropertyName;

            if (Store.IsDate(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Date;
            }
            else if (Store.IsInteger(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Int;
            }
            else if (Store.IsFloat(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Float;
            }
            else if (Store.IsBoolean(propertyMetadata.ModelType))
            {
                modelField.Type = ModelFieldType.Boolean;
            }
            else if (propertyMetadata.ModelType == typeof(string))
            {
                modelField.Type = ModelFieldType.String;
            }

            if (propertyMetadata.IsComplexType && modelAttr != null)
            {
                modelField.IsComplex = true;
            }

            if (modelAttr != null && storeModel != null)
            {
                modelAttr.CopyTo(modelField, storeModel);
            }

            if (storeModel != null)
            {
                storeModel.Fields.Add(modelField);
            }

            ValidationCollection validations = new ValidationCollection();

            if (propertyMetadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY))
            {
                validations = (ValidationCollection)propertyMetadata.AdditionalValues[AbstractValidationAttribute.KEY];
            }

            foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext))
            {
                var rule = v.GetClientValidationRules().FirstOrDefault();

                if (rule != null)
                {
                    switch (rule.ValidationType)
                    {
                    case "required":
                        validations.Add(new PresenceValidation {
                            Message = rule.ErrorMessage
                        });
                        break;

                    case "regex":
                        if (rule.ValidationParameters.ContainsKey("pattern"))
                        {
                            validations.Add(new FormatValidation {
                                Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage
                            });
                        }
                        break;

                    case "length":
                        int num;
                        LengthValidation length = new LengthValidation();
                        length.Message = rule.ErrorMessage;
                        if (rule.ValidationParameters.ContainsKey("max"))
                        {
                            if (int.TryParse(rule.ValidationParameters["max"].ToString(), out num))
                            {
                                length.Max = num;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        if (rule.ValidationParameters.ContainsKey("min"))
                        {
                            if (int.TryParse(rule.ValidationParameters["min"].ToString(), out num))
                            {
                                length.Min = num;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        validations.Add(length);
                        break;
                    }
                }
            }

            if (controllerContext != null)
            {
                foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext))
                {
                    var rule = v.GetClientValidationRules().FirstOrDefault();

                    if (rule != null)
                    {
                        switch (rule.ValidationType)
                        {
                        case "required":
                            validations.Add(new PresenceValidation {
                                Field = modelField.Name, Message = rule.ErrorMessage
                            });
                            break;

                        case "regex":
                            if (rule.ValidationParameters.ContainsKey("pattern"))
                            {
                                validations.Add(new FormatValidation {
                                    Field = modelField.Name, Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage
                                });
                            }
                            break;

                        case "length":
                            var lengthVal = new LengthValidation();
                            if (rule.ValidationParameters.ContainsKey("max"))
                            {
                                lengthVal.Max = (int)Convert.ChangeType(rule.ValidationParameters["max"], typeof(int));
                            }

                            if (rule.ValidationParameters.ContainsKey("min"))
                            {
                                lengthVal.Min = (int)Convert.ChangeType(rule.ValidationParameters["min"], typeof(int));
                            }

                            lengthVal.Message = rule.ErrorMessage;
                            validations.Add(lengthVal);
                            break;
                        }
                    }
                }
            }

            if (validations.Count > 0)
            {
                if (storeModel != null)
                {
                    storeModel.Validations.AddRange(validations);
                }
                else
                {
                    modelField.Validations = validations;
                }
            }

            return(modelField);
        }