Create a custom validation rule

Create a custom validation rule

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


lazaroms posted on Friday, July 02, 2010

Hi all,

I have a class (Person) inheriting from BusinessBase.

Person has a property called DateofBirth.

I need to create a validation rule to prevent that DateofBirth won't be greater than current date.

How can create a custom rule to integrated to de class validation rules through the ValidationRules.Addrule?

Please, any code in VB.NET will appreciated.

 

Thanks,

 

L.M

Marjon1 replied on Saturday, July 03, 2010

The books do a very good job of explaining how to create these rules, as do the CSLA videos. 
I would recommend both exploring both of those resources, if you haven't previously.

You also don't mention what version of CSLA you are using / targeting. 
There is a fundamental difference in how rules are created in version 3.x and in the upcoming version 4.

Here is a basic rule that will compare any standard date field and ensure that it is not greater than the standard date, if this is a rule that may be applied to multiple objects it is good practice to put it into your own common rules library.

Private Shared Function DateNotGreaterThanCurrentDate(ByVal target as Object, ByVal e as Csla.Validation.RuleArgs) As Boolean

If DirectCast(CallByName(target, e.PropertyName, MethodType.Get, Nothing), Date) >= Date.Now()) Then
    e.Description = String.Format("The {0} field cannot be greater than the current date", e.PropertyName)
    Return False
End If

Return True

End Function 

lazaroms replied on Saturday, July 03, 2010

Hi Marjon:

 

Thank you for your help.

I'm a rookie and I'm spanish spoken. I red the book but I don't undestarnd much about custom rules, I'll do it again.

Please could you tell where can I find the videos?

Thanks again,

 

L.M

 

ajj3085 replied on Saturday, July 03, 2010

The videos can be purchased at http://store.lhotka.net/

Copyright (c) Marimer LLC