CSLA 2.1 New Validation Rule Handler

CSLA 2.1 New Validation Rule Handler

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


Vinodonly posted on Monday, November 06, 2006

I'm having my validation rule handler with following signature

Private Shared Function OrderCfmDtChk(Of T As Order)(ByVal target As T, _

ByVal e As Validation.RuleArgs) As Boolean

here the e parameter is also a subclass of RuleArgs and thus provides additional parameter, I'm trying to add this rule with following line but is giving a compile time error :-

ValidationRules.AddRule(Of Order)(AddressOf OrderCfmDtChk(Of Order), _

New DaysValueRuleArgs("OrderCfmDt", 7))

 

Can anybody help in this..

 

Vinodonly replied on Tuesday, November 07, 2006

can anybody help on this.

If i don't use the custom RuleArgs then below given line is working fine, and is performing validation also.

ValidationRules.AddRule(Of Order)(AddressOf OrderCfmDtChk(Of Order), "OrderCfmDt")

but in this case i have to hard code value inside the validator, whereas i want to pass additional parameters through custom Rule Args as specified in my initial post.

ajj3085 replied on Tuesday, November 07, 2006

You need to create your own subclass of RuleArgs and have your function accept that.

Vinodonly replied on Tuesday, November 07, 2006

Kindly note i have done that and this is the method signature for the same

 

Private Shared Function OrderCfmDtChk(Of T As Order)(ByVal target As T, _

ByVal e As Validation.RuleArgs) As Boolean

 

But when i use following line to add this rule, it is giving compile timer error :-

 

ValidationRules.AddRule(Of Order)(AddressOf OrderCfmDtChk(Of Order), _

New DaysValueRuleArgs("OrderCfmDt", 7))

 

Can you pls help me in telling what code i should exactly write so this rule can be added.

Vinodonly replied on Tuesday, November 07, 2006

Further to my previous message, this is the custom Rule Args class, Validation rule handler converts the passed e parameter to this class by using following code

Dim Days As Integer = CType(e, DaysValueRuleArgs).Days

 

 

Public Class DaysValueRuleArgs

Inherits Csla.Validation.RuleArgs

Private mDays As Integer

Public ReadOnly Property Days() As Integer

Get

Return mDays

End Get

End Property

Public Sub New(ByVal propertyName As String, ByVal Days As Integer)

MyBase.New(propertyName)

mDays = Days

End Sub

Public Overrides Function ToString() As String

Return MyBase.ToString & "?Days=" & mDays.ToString

End Function

End Class

RockfordLhotka replied on Tuesday, November 07, 2006

You need to use the AddRule(Of T, R)() overload, so you can specify the type of both the target and arguments parameters.

Vinodonly replied on Wednesday, November 08, 2006

please excuse me for nagging so much for this problem but i'm still not able to get rid of compiler errors. can u pls help me in advising what is the exact line to write through which this rule can be added, pls help

Here is the summary of my previous posts.

Custom Rules Args :- DaysValueRuleArgs

Validation routine :- OrderCfmDtChk (Signature was given in my previous post)

 

I'm writing following line currently and it is giving compile time error :-

 

ValidationRules.AddRule(Of Order)(AddressOf OrderCfmDtChk(Of Order), _

New DaysValueRuleArgs("OrderCfmDt", 7))

 

 

ajj3085 replied on Wednesday, November 08, 2006

From Rocky's comments, I think you need

ValidationRules.AddRule( Of Order, Of DayValueRuleArgs )

Vinodonly replied on Wednesday, November 08, 2006

Finally it is working but i had to do major changes. I'm pasting main points here. Please Refer to

MaxValue and  MaxValueRuleArgs for more info

This is the AddRule line :-

ValidationRules.AddRule(AddressOf OrderCfmDtChk(Of Integer), _

New DaysValueRuleArgs(Of Integer)("OrderCfmDt", 7))

Here is the validator signature and few important lines of code to retreive property value & parameter value :-

 

Private Shared Function OrderCfmDtChk(Of T As IComparable)(ByVal target As Object, _

ByVal e As Validation.RuleArgs) As Boolean

Dim PropValue As String = CStr(CallByName(target, e.PropertyName, CallType.Get))

Dim Days As Integer = DirectCast(e, DaysValueRuleArgs(Of Integer)).Days

 

& Finally here is the custom rule args and it's new constructor

Public Class DaysValueRuleArgs(Of T)

........

Public Sub New(ByVal propertyName As String, ByVal Days As Integer)

 

I hope this will help if somebody else is trying this...

Copyright (c) Marimer LLC