Comparison - LessThan/GreaterThan

Dec 2, 2009 at 12:51 AM

Hi guys

Just wondering how far away the fix for Comparison is to add the required functionality for LessThan/GreaterThan support is...

This has been a long time coming and we have a very great need to ensure that start dates aren't greater than end dates and there would seem to be no good solution at this time.

If LessThan/GreaterThan support is still a long way off, how do we deal like this at the moment.

Cheers

Anthony

Mar 1, 2010 at 5:20 PM

Anthony,

I was looking for something similar and created a new validation attribute for "GreaterThan". This is not been fully baked, so no warranty on the use, but it seems to work in my app so far.

    public class GreaterThanAttribute : ValidationAttribute, ICustomRule
    {
        private int TheNumber { get; set; }
        public GreaterThanAttribute(int theNumber)
        {
            TheNumber = theNumber;
            ErrorMessage = "*";
        }

        public override bool IsValid(object value)
        {
            if(!(value is int))
            {
                return false;
            }
            return ((int) value > TheNumber);
        }
        public CustomRule ToCustomRule()
        {
            return new CustomRule(
                "GreaterThanN",            // JavaScript function name
                new { theNumber = TheNumber }, // Params for JavaScript function
                ErrorMessage               // Message if invalid
            );
        }
    }

The usage of this...

 [Required]
 [GreaterThan(0)]
 public int Priority { get; set; }