Simplifying the process of adding common rules

Simplifying the process of adding common rules

Old forum URL: forums.lhotka.net/forums/t/1149.aspx


David posted on Thursday, September 07, 2006

I have a lot of business rules to add for time and date validation and I am trying to write some code that will simplify the addition of these rules. I am trying to use the new generic common rules from 2.1 which help, but I have the following issue:

I am adding a rule to test that a time value does not exceed 6pm as follows. I have split this up into several lines to make it more readable:

 Dim MyRuleArg As New MaxValueRuleArgs(Of DateTime)("FinishTime", TimeSerial(18, 0, 0))
 ValidationRules.AddRule(AddressOf MaxValue(Of DateTime), MyRuleArg)

This works, but the message is not so good: "FinishTime can not exceed 1/01/0001 6:00:00PM"

Specifically I need to do something about the way it is formatting my maximum time value. Is there an easy way to do this?

 

JoOfMetL replied on Thursday, September 07, 2006


A solution would be to create your own MaxValue rule. What would allow you to personalize the message.

SonOfPirate replied on Thursday, September 07, 2006

Another option, if you are willing to alter the default CSLA objects, is to provide an overloaded constructor to the MaxValueRuleArgs class that accepts a format string.  Then you can simply add such a string to the code you've included and accomplish your goal.  (You'll want to change the ToString method to use the format string, too!)

JoOfMetL replied on Thursday, September 07, 2006

Yes  it's a better solution

RockfordLhotka replied on Thursday, September 07, 2006

Yeah, that's a very good idea - I'll add it to my list of "things to do in a future release"

David replied on Thursday, September 07, 2006

I looked into this suggestion (overloaded constructor to the MaxValueRuleArgs class) but there is one problem. When the code in the MaxValue function formats the message, it declares a variable of type T, assigns the max value to this variable and then calls the ToString method. So I have no way of intercepting the formatting process.

So what I did was to create my own MaxValue function as well as my own MaxValueRuleArgs(OF T) class. I then added a read only property FormattedMaxValue to MaxValueRuleArgs that inspected the current type and value and did the type specific formatting I required. I then altered MaxValue to call this property instead of the current method (using ToString).

This works nicely for me, and rather than changing CSLA I have just renamed these and included the code in my project.

 

Copyright (c) Marimer LLC