protected override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable <Attribute> attributes)
    {
        List <ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList();

        // inject our new validator
        if (metadata.ModelType.Name == "DateTime")
        {
            DataAnnotationsModelValidationFactory factory;
            RegularExpressionAttribute            regex = new RegularExpressionAttribute(
                "^(((0?[1-9]|1[012])/(0?[1-9]|1\\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\\d)\\d{2}|0?2/29/((19|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$");
            regex.ErrorMessage = "Invalid date format";
            if (!AttributeFactories.TryGetValue(regex.GetType(), out factory))
            {
                factory = DefaultAttributeFactory;
            }
            vals.Add(factory(metadata, context, regex));
        }
        return(vals.AsEnumerable());
    }
Beispiel #2
0
    protected override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable <Attribute> attributes)
    {
        if (!attributes.Any(i => i is RegularExpressionAttribute))
        {
            if (typeof(DateTime).Equals(metadata.ModelType) || (metadata.ModelType.IsGenericType && typeof(DateTime).Equals(metadata.ModelType.GetGenericArguments()[0])))
            {
                DataAnnotationsModelValidationFactory factory;
                RegularExpressionAttribute            regex = null;
                switch (metadata.DataTypeName)
                {
                case "Date":
                    regex = new RegularExpressionAttribute(RegExPatterns.Date)
                    {
                        ErrorMessage = "Invalid date. Please use a m/d/yyyy format"
                    };
                    break;

                case "Time":
                    regex = new RegularExpressionAttribute(RegExPatterns.Time)
                    {
                        ErrorMessage = "Invalid time. Please use a h:mm format"
                    };
                    break;

                default:         //DateTime
                    regex = new RegularExpressionAttribute(RegExPatterns.DateAndTime)
                    {
                        ErrorMessage = "Invalid date / time. Please use a m/d/yyyy h:mm format"
                    };
                    break;
                }

                if (!AttributeFactories.TryGetValue(regex.GetType(), out factory))
                {
                    factory = DefaultAttributeFactory;
                }
                yield return(factory(metadata, context, regex));
            }
        }
    }
Beispiel #3
0
        protected override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable <Attribute> attributes)
        {
            List <ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList();
            DataAnnotationsModelValidationFactory factory;

            // Inject our new validator.
            if (metadata.ContainerType != null)
            {
                // Check if we have validation for this class name.
                if (ValidationManager.Validators.ContainsKey(metadata.ContainerType.Name))
                {
                    var validator = ValidationManager.Validators[metadata.ContainerType.Name];

                    // Check if we have validation for this property name.
                    if (validator.ContainsKey(metadata.PropertyName))
                    {
                        var property = validator[metadata.PropertyName];

                        // Only add validation to visible properties.
                        if (property.Visible)
                        {
                            // Required attribute.
                            if (property.Required)
                            {
                                ValidationAttribute required;

                                if (metadata.ModelType == typeof(bool))
                                {
                                    // For required booleans, enforce true.
                                    required = new EnforceTrueAttribute {
                                        ErrorMessage = property.ErrorMessage
                                    };
                                }
                                else if (metadata.ModelType == typeof(int) || metadata.ModelType == typeof(long) || metadata.ModelType == typeof(double) || metadata.ModelType == typeof(float))
                                {
                                    // For required int, long, double, float (dropdownlists), enforce > 0.
                                    required = new GreaterThanZeroAttribute()
                                    {
                                        ErrorMessage = property.ErrorMessage
                                    };
                                }
                                else
                                {
                                    required = new RequiredAttribute {
                                        ErrorMessage = property.ErrorMessage
                                    };
                                }

                                if (!AttributeFactories.TryGetValue(required.GetType(), out factory))
                                {
                                    factory = DefaultAttributeFactory;
                                }

                                yield return(factory(metadata, context, required));
                            }

                            // Regular expression attribute.
                            if (!string.IsNullOrEmpty(property.RegularExpression))
                            {
                                RegularExpressionAttribute regEx = new RegularExpressionAttribute(property.RegularExpression)
                                {
                                    ErrorMessage = property.ErrorMessage
                                };

                                if (!AttributeFactories.TryGetValue(regEx.GetType(), out factory))
                                {
                                    factory = DefaultAttributeFactory;
                                }

                                yield return(factory(metadata, context, regEx));
                            }

                            // Compare attribute.
                            if (!string.IsNullOrEmpty(property.Compare))
                            {
                                CompareAttribute compare = new CompareAttribute(property.Compare)
                                {
                                    ErrorMessage = property.ErrorMessage
                                };

                                if (!AttributeFactories.TryGetValue(compare.GetType(), out factory))
                                {
                                    factory = DefaultAttributeFactory;
                                }

                                yield return(factory(metadata, context, compare));
                            }
                        }
                    }
                }
            }
        }