Wednesday, September 14, 2011

Custom Validation Attributes


while using data annotation we face difficulty for adding validation for Min and max value or for Start date and End Date. 
Validation attributes for min and Max, Startdate and Enddate
i have created Custom validation attributes which will deal with min and max values and datetime as well.

Lessthan validation attribtue 

[AttributeUsage(AttributeTargets.Property)]
  public class Lessthan : ValidationAttribute
    {
        private readonly string _dependentFieldName;
        private readonly bool _considerEqualValue;
        private PropertyType _propertyType;

        #region Constructors
        public Lessthan(string propertyName)
            : this(PropertyType.Numeric)
        {
            _dependentFieldName = propertyName;
        }

        public Lessthan(string propertyName, bool considerEqualValue)
            : this(propertyName, PropertyType.Numeric)
        {
            _considerEqualValue = considerEqualValue;
        }

        public Lessthan(string propertyName, PropertyType propertyType)
            : this(propertyType)
        {
            _dependentFieldName = propertyName;
        }

        public Lessthan(string propertyName, bool considerEqualValue, PropertyType propertyType)
            : this(propertyName, propertyType)
        {
            _considerEqualValue = considerEqualValue;
        }

        private Lessthan(PropertyType propertyType)
        {
            _propertyType = propertyType;
        }
        #endregion

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
                return new ValidationResult(base.ErrorMessage);
            PropertyInfo dependentProperty = validationContext.ObjectType.GetProperty(_dependentFieldName);
            if (dependentProperty == null)
                throw new ValidationException("No property found " + _dependentFieldName);
            switch (_propertyType)
            {
                case PropertyType.Numeric:

                    double currentAttributeValue;
                    if (!double.TryParse(value.ToString(), out currentAttributeValue))
                    {
                        throw new ValidationException("Numeric Value Expected");
                    }
                    double _dependentFieldValue;
                    var dval = dependentProperty.GetValue(validationContext.ObjectInstance, null);
                    if (dval == null)
                        return new ValidationResult(base.ErrorMessage);
                    if (!double.TryParse(dval.ToString(), out _dependentFieldValue))
                    {
                        throw new ValidationException("Numeric Value Expected for" + _dependentFieldName);
                    }

                    if (currentAttributeValue < _dependentFieldValue)
                        return ValidationResult.Success;

                    if (_considerEqualValue && currentAttributeValue <= _dependentFieldValue)
                        return ValidationResult.Success;
                    return new ValidationResult(base.ErrorMessage);

                case PropertyType.DateTime:

                    DateTime currentValue;
                    if (!DateTime.TryParse(value.ToString(), out currentValue))
                    {
                        throw new ValidationException("DateTime type Expected");
                    }
                    DateTime _dependentFieldDTValue;
                    var dtval = dependentProperty.GetValue(validationContext.ObjectInstance, null);
                    if (dtval == null)
                        return new ValidationResult(base.ErrorMessage);
                    if (!DateTime.TryParse(dtval.ToString(), out _dependentFieldDTValue))
                    {
                        throw new ValidationException("DateTime type Expected" + _dependentFieldName);
                    }

                    if (currentValue < _dependentFieldDTValue)
                        return ValidationResult.Success;

                    if (_considerEqualValue && currentValue <= _dependentFieldDTValue)
                        return ValidationResult.Success;
                    return new ValidationResult(base.ErrorMessage);
                default:
                    return ValidationResult.Success;
            }

        }
    }




Greater than validation attribute

[AttributeUsage(AttributeTargets.Property)]
    public class Greaterthan : ValidationAttribute
    {
        private readonly string _dependentFieldName;
        private readonly bool _considerEqualValue;
        private PropertyType _propertyType;

        public Greaterthan(string propertyName)
            : this(PropertyType.Numeric)
        {
            _dependentFieldName = propertyName;
        }

        public Greaterthan(string propertyName, PropertyType propertyType)
            : this(propertyType)
        {
            _dependentFieldName = propertyName;
        }

        public Greaterthan(string propertyName, bool considerEqualValue)
            : this(propertyName)
        {
            _considerEqualValue = considerEqualValue;
        }

        public Greaterthan(string propertyName, bool considerEqualValue, PropertyType propertyType)
            : this(propertyName, propertyType)
        {
            _considerEqualValue = considerEqualValue;
        }

        private Greaterthan(PropertyType propertyType)
        {
            _propertyType = propertyType;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
                return new ValidationResult(base.ErrorMessage);
            PropertyInfo dependentProperty = validationContext.ObjectType.GetProperty(_dependentFieldName);
            if (dependentProperty == null)
                throw new ValidationException("No property found " + _dependentFieldName);

            switch (_propertyType)
            {
                case PropertyType.Numeric:
                    double currentAttributeValue;
                    if (!double.TryParse(value.ToString(), out currentAttributeValue))
                        throw new ValidationException("Numeric Value Expected");

                    double _dependentFieldValue;
                    var dval = dependentProperty.GetValue(validationContext.ObjectInstance, null);
                    if (dval == null)
                        return new ValidationResult(base.ErrorMessage);
                    if (!double.TryParse(dval.ToString(), out _dependentFieldValue))
                    {
                        throw new ValidationException("Numeric Value Expected for" + _dependentFieldName);
                    }

                    if (currentAttributeValue > _dependentFieldValue)
                        return ValidationResult.Success;

                    if (_considerEqualValue && currentAttributeValue >= _dependentFieldValue)
                        return ValidationResult.Success;

                    return new ValidationResult(base.ErrorMessage);
                case PropertyType.DateTime:
                    DateTime currentValue;
                    if (!DateTime.TryParse(value.ToString(), out currentValue))
                        throw new ValidationException("DateTime Type Expected");

                    DateTime _dependentValue;
                    var dtval = dependentProperty.GetValue(validationContext.ObjectInstance, null);
                    if (dtval == null)
                        return new ValidationResult(base.ErrorMessage);
                    if (!DateTime.TryParse(dtval.ToString(), out _dependentValue))
                    {
                        throw new ValidationException("DateTime Type Expected for" + _dependentFieldName);
                    }

                    if (currentValue > _dependentValue)
                        return ValidationResult.Success;

                    if (_considerEqualValue && currentValue == _dependentValue)
                        return ValidationResult.Success;

                    return new ValidationResult(base.ErrorMessage);
                default:
                    return ValidationResult.Success;
            }

        }
    }

No comments:

Post a Comment