CodeSmith Community
Your Code. Your Way. Faster!

YAVQ (Yet another validation question)

Latest post 11-01-2006 5:45 PM by bgjohnso. 3 replies.
  • 10-31-2006 7:05 AM

    YAVQ (Yet another validation question)

    We are having two pretty specific problems with validation rules that hopefully someone can provide quick advice on.  I browsed several of the other validation topics, but didn't see an answer (or what I thought was an answer).  The first problem has to do with DateTimes.  When I use a snippet like this:

    // LastAccessedDate < DateTime.Now
    CommonRules.CompareValueRuleArgs<DateTime> lastAccessedDateRuleArgs = new CommonRules.CompareValueRuleArgs<DateTime>(PropertyNames.LastAccessedDate, DateTime.UtcNow);
    lastAccessedDateRuleArgs.Description =
    string.Format(ErrorMessages.LastAccessedDateInvalid, Snippets.AQuoteConfiguration);
    ValidationRules.AddRule(
    CommonRules.LessThanOrEqualToValue<DateTime>, lastAccessedDateRuleArgs);

    I have a problem (other than syntax color highlighting that apparently doesn't want to copy over from VS.)  It looks to me as if the DateTime.UtcNow is evaluated when the rule is added, which apparently happens when the validation rules are accessed for the first time.  We're having a problem, however, with the fact that if we set that property to DateTime.UtcNow after the rule has been added, the validation fails because the property usage of DateTime.UtcNow is greater than DateTime.UtcNow when the rule was added.  Is that a clear enough description?  Is there a remedy?

     We have a similar problem when I want to validate certain things conditionally - say either xxxId must be greater than 0, or xxxIdSource must not be null.  I can part of the conditionality by saying if (xxxIdSource == null) ValidationRules.AddRule(ruleToValidateXxxId>0) but I'm having issues validating xxxIdSource as the else.  Again, it appears to be an issue with the rules being added at one point in time.

     My guess (hope) is that there is an easy answer to these questions.  If anyone could help me out, I'd really appreciate it.

    Thanks,
    Mark Stafford

    • Post Points: 60
  • 10-31-2006 10:55 AM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: YAVQ (Yet another validation question)

    Mark,

    You will need to use delegates to define your own validation rules.  Have you taken a look at the documentation on the wiki?

    http://wiki.nettiers.com/entitylayer

    It includes information on using the validation rules engine.  You can use custom delegates to make your validation as complex as necessary.  You can just override the AddValidationRules method and inject your own rules.  The example below will evaluate the OrderDate against the current date at the time the validation is made, and not when the rule is first added.

    /// <summary>

    /// Adds custom validation rules to this object.

    /// </summary>

    protected override void AddValidationRules()

    {

        base.AddValidationRules();

     

        //Add custom validation rules

        ValidationRules.AddRule(ValidateOrderDate, "OrderDate");

    }

     

    /// <summary>

    /// Validates the order date.

    /// </summary>

    /// <param name="target">The target.</param>

    /// <param name="e">The e.</param>

    /// <returns></returns>

    private bool ValidateOrderDate(object target, Validation.ValidationRuleArgs e)

    {

        if ((this.OrderDate ?? DateTime.MinValue) > DateTime.Today)

        {

            e.Description = "The Order Date must not be in the future.";

            return false;

        }

        return true;

    }

    Hope this helps.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 11-01-2006 5:29 PM In reply to

    Re: YAVQ (Yet another validation question)

    It is helpful... but I don't think it answers my question.  The primary question I had regarding DateTime had to do with DateTime.UtcNow.  What appears to be happening is that DateTime.UtcNow (or DateTime.Now) is evaluated only when the rule is added.  I wrote a compare rule specifying that the last accessed date had to be less than DateTime.UtcNow.Date, but let's say I wanted to record the LastAccessedTime.  I think that DateTime.Now is being evaluated when the validation rule is added.

    Consider the following:

    1. Person record is pulled from database at 3:15:28.492 on a given day.

    2. Validation rule is added when the Person record is created, and if I specify that the LastAccessedTime property must be less than DateTime.Now, I think DateTime.Now gets evaluated at that point (3:15:28.495, maybe).

    3. Person record is modified, and LastAccessedTime is set to 3:15:28.502 - which should be exactly the same as DateTime.Now if DateTime.Now were evaluated at the same time that the validation rule is enforced.

    4. Validation rule fails because LastAccessedTime is greater than what was DateTime.Now when the rule was added.

    And now that I've gone through that rather lengthy explanation, maybe what you said does answer my question, because the usage of DateTime.Today in that example wouldn't be evaluated until the rule fires.  Well, for the record, it seems like that's one class of rule that should be added to common rules.  Other rules that we've been working on internally that we would eventually like to see added are recursive-property rules (validate that this.ShoppingCart.Items.Count > 2, for instance) and collection/list rules (at least one of these items must be xxx).  I guess that's all from my end.  If I haven't thought this out properly, please let me know.

     Mark

    • Post Points: 5
  • 11-01-2006 5:45 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    RE: YAVQ (Yet another validation question)

    Mark,
     
    You are correct in that DateTime.UtcNow gets evaluated when the rules gets added, because you are passing in the result of that function and not the function itself.  The only way I know of to get around this is to use a delegate like I showed before.  As for the common rules, if you come up with a good rule that is generic in use, submit it in the Contributions and Patches forum and we'll look at getting it added.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

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