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?
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!)
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