CodeSmith Community
Your Code. Your Way. Faster!

Validation across properties in same class

Latest post 10-23-2007 4:01 AM by LosManos. 5 replies.
  • 11-27-2006 3:03 PM

    Validation across properties in same class

    I saw a couple of threads about validating across classes, but I need something simpler.

    I have an object.StartDate and object.EndDate, and I'd like to ensure that the StartDate is less than or equal to the EndDate.

    I tried associating a custom rule StartIsLessThanOrEqualToEnd with both properties, but that doesn't do what I want.  If user sets the StartDate first, that rule gets broken, and does not get re-checked when user sets the EndDate.  So, the IsValid is false, but the rule is not broken.

    Is there a way to call an "object-level" validation, as opposed to a "property-level" validation?  What's the best way to handle this scenario?

    Dave

    Filed under: ,
    • Post Points: 65
  • 11-27-2006 3:16 PM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 922
    • Points 34,710

    Re: Validation across properties in same class

    Reply |Contact |Answer

    If you don't want the rules to be processed whilst setting the properties you can set the SuppressEntityEvents to true.

    If you have both of your properties set and SuppressEntityEvents = false, then call object.Validate() and the rules should be validated correctly and the IsValid set accordingly.

    HTH

    swin

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 11-27-2006 3:18 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 726
    • Points 16,910

    Re: Validation across properties in same class

    Dave,

    One way to define your rule would be like following:

             ...
            
            protected override void AddValidationRules()
            {
                base.AddValidationRules();
                ValidationRules.AddRule(ValidateDates, ClassColumn.StartDate.ToString());
            }
            private bool ValidateDate(ValidateDates target, Validation.ValidationRuleArgs e)
            {
                string errorMessage = "";
                if (Util.DateDiff(this.Start, this.EndDate) > 0)
                {
                    errorMessage = @"The StartDate can only be less or equal to EndDate.";
                    e.Description = errorMessage;
                    return false;
                }
                return true;
            }        
            
            ...
            
        public static System.Double DateDiff(DateTime date1, DateTime date2)
        {
            DateTime d1 = new DateTime(date1.Year, date1.Month, date1.Day);
            DateTime d2 = new DateTime(date2.Year, date2.Month, date2.Day);
            TimeSpan s = d2 - d1;
            return (s.TotalDays);
        } 

    davem1958@gmail.com:

    I saw a couple of threads about validating across classes, but I need something simpler.

    I have an object.StartDate and object.EndDate, and I'd like to ensure that the StartDate is less than or equal to the EndDate.

    I tried associating a custom rule StartIsLessThanOrEqualToEnd with both properties, but that doesn't do what I want.  If user sets the StartDate first, that rule gets broken, and does not get re-checked when user sets the EndDate.  So, the IsValid is false, but the rule is not broken.

    Is there a way to call an "object-level" validation, as opposed to a "property-level" validation?  What's the best way to handle this scenario?

    Dave

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 5
  • 11-27-2006 3:21 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 726
    • Points 16,910

    Re: Validation across properties in same class

    Oops ... sorry i misread the initial post. I agree with Swin though

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 5
  • 11-27-2006 3:58 PM In reply to

    Re: Validation across properties in same class

    I think that answers my question.  Basically, it's a "lazy-validate" implementation, and IsValid is not always "valid".  Smile
    • Post Points: 35
  • 10-23-2007 4:01 AM In reply to

    • LosManos
    • Not Ranked
    • Joined on 03-30-2007
    • Posts 3
    • Points 15

    Re: Validation across properties in same class

    hejdig. I created a slightly simpler example. (Since I got help from the original posting I thought help later readers.)

    Also note that one has to add a rule for both Min and Max and that there will be two myEntity.BrokenRulesList items.

        protected override void AddValidationRules()
        {
            base.AddValidationRules();
            ValidationRules.AddRule(ValidateMinMax, "Min");
            ValidationRules.AddRule(ValidateMinMax, "Max");
        }

        public bool ValidateMinMax( object o, Validation.ValidationRuleArgs e)
        {
            if (min < max)
            {
                return true;
            }
            else
            {
                e.Description = string.Format("Min [{0}] must be less than Max [{0}]", min, max);
                return false;
            }
        }

    /OF
    Filed under: ,
    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems