Validate Date based on another property

Topics: Validation Application Block
Dec 23, 2009 at 7:28 AM

Hi,

I have 2 date columns, purchase date and warranty expiry date.

I should include a condition such that warranty expiry date is atleast 6 months later than purchase date.

How should I define this rule.

Thanks

Dec 23, 2009 at 9:21 AM

Hi,

I see two ways of doing that. One is to implement a SelfValidation which you will add your custom logic of comparing the values. Please see this: http://msdn.microsoft.com/en-us/library/cc309328.aspx . The other one is to use the PropertyComparisonValidator, As shown below, I have another extra property which returns the purchase date plus 6 months where the warranty expirydate will be checked if its greater than that property.

public class ClassToValidate
    {
        private DateTime purchaseDate;

        public DateTime PurchaseDate
        {
            get { return purchaseDate; }
            set { purchaseDate = value; }
        }

        private DateTime warrantyExpiryDate;
        [PropertyComparisonValidator("CompareDate", ComparisonOperator.GreaterThan,
            MessageTemplate="Must be at least six (6) months greater than the purchase date.")]
        public DateTime WarrantyExpiryDate
        {
            get { return warrantyExpiryDate; }
            set { warrantyExpiryDate = value; }
        }

        public DateTime CompareDate
        {
            get
            {
                return purchaseDate.AddMonths(6);
            }
        }

    }

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 28, 2009 at 9:52 AM

Thanks.

That one worked.