Validation.Commonrules decimal type problem

Validation.Commonrules decimal type problem

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


JonnyBergdahl posted on Tuesday, October 17, 2006

I added some validationrules like this:

ValidationRules.AddRule(Csla.Validation.CommonRules.MaxValue<decimal>, 
   
new Csla.Validation.CommonRules.MaxValueRuleArgs<decimal>("GrossWeight", 99999.99m));

The code happily compiles and executes until I set the GrossWeight property. It then fails, with an exception with generic description like Unable to set property, that is hiding the real cause of the exception: throw ArgumentException(Resources.PrimitiveTypeRequired);

Why isn't decimal supported in the CommonRules class, seems easy enough?
Why is the real exception hidden, making med debug this for 30 minutes before I found out what was wrong?

Regards;
/jb

ajj3085 replied on Tuesday, October 17, 2006

What is your GrossWeight property type?  I've used MinValue for decimal and it works just fine.

SonOfPirate replied on Wednesday, October 18, 2006

Not sure what is leading up to the exception during runtime, but the statement you've included does make me wonder if some kind of a Type.IsPrimitive condition is being evaluated (use of Resource.PrimitiveTypeRequired as the exception message).

If this is the case, then the condition will fail for System.Decimal (decimal) because it is not considered a primitive type.  See if that helps.

 

JoeFallon1 replied on Thursday, October 19, 2006

As I recall the check for IsPrimitiveType did exist in an older implementation of this rule

But Rocky updated it in 2.1 to this:

Public Function MaxValue(Of T As IComparable)(ByVal target As Object, ByVal e As RuleArgs) As Boolean
 
Dim pi As PropertyInfo = target.GetType.GetProperty(e.PropertyName)
 
Dim value As T = DirectCast(pi.GetValue(target, Nothing), T)
 
Dim max As T = DirectCast(e, MaxValueRuleArgs(Of T)).MaxValue
 
Dim result As Integer = value.CompareTo(max)

  If result >= 1 Then
   
e.Description = String.Format(My.Resources.MaxValueRule, e.PropertyName, max.ToString)
 
Else
   
Return True
 
End If
End Function

However, I did a quick test and found a problem with the use of DirectCast. I had a large Integer value that could not be DirectCast to a Decimal value. But CType of the same Integer value to Decimal worked fine.

So I think this is a bug in the latest version which needs to be fixed.

Joe

Copyright (c) Marimer LLC