Help with validation

Help with validation

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


griff posted on Tuesday, February 05, 2008

Hi

I have done this so far:

Protected Overrides Sub AddBusinessRules()

ValidationRules.AddRule(Of Q1)(AddressOf ValueNotZero(Of Q1), "InWork")

ValidationRules.AddRule(Of Q1)(AddressOf ValueNotZero1(Of Q1), "OtherHouseholderWorking")

End Sub

Private Shared Function ValueNotZero(Of T As Q1)( ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean

If target.m_inWork = 0 Then

e.Description = "1) Please answer this question - In/out Work?"

Return False

Else

Return True

End If

Return False

End Function

Private Shared Function ValueNotZero1(Of T As Q1)( ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean

If target.m_otherHouseholderWorking = 0 Then

e.Description = "8a) Please answer this question"

Return False

Else

Return True

End If

End Function

But I have serveral more fields that require the same validation i.e. > 0. I didn't want to repeat ValueNotZero2,3,4,5....... and so on

How can I combine into one ValueNotZero function and implement.  My guess I'm being somewhat thick here.

Thanks

Richard

 

JoeFallon1 replied on Tuesday, February 05, 2008

Richard,

You can do this by moving the rule into a class like Rocky's Common Rules. I basically copied his rules into my own class and namespace and then continued to add my own.

So you might have a rule like this:

Public Function KeyNotZero(ByVal target As Object, ByVal e As RuleArgs) As Boolean
 
Dim ruleArg As myRuleArgs = DirectCast(e, myRuleArgs)
 
Dim propertyName As String = ruleArg.PropertyName
  

 
Dim value As Integer= CInt(CallByName(target, propertyName, CallType.Get))

 
If value = 0 Then
   
e.Description = propertyName & " cannot be 0."
    
Return False
 
Else
   
Return True
 
End If
End Function

This rule can call other functions to determine e.Description if you need to vary it.

Joe

griff replied on Wednesday, February 06, 2008

Thanks for the reply.

I have copied in common rules and added in your function - but I'm having difficulty calling the validation

ValidationRules.AddRule(AddressOf CustomValidation.KeyNotZero(Of Q1), New CustomValidation.RuleArgs("InWork", "1. Are you in work"), "InWork")

How would you call?

Thanks

Richard

 

 

 

 

JoeFallon1 replied on Wednesday, February 06, 2008

You can remove the (OfQ1) as it is not required for general rules.

I call it like this:

ValidationRules.AddRule(AddressOf KeyNotZero, New MyRuleArgs("AKeyPropertyName"))

If you have extra parameters in your rule args you might call it like this:

ValidationRules.AddRule(AddressOf KeyNotZero, New MyRuleArgs("InWork", "1. Are you in work"))

Joe

griff replied on Wednesday, February 06, 2008

Hi

thank Joe, I have done this but get compile error

Error 1 Overload resolution failed because no accessible 'AddRule' can be called with these arguments:
    'Public Sub AddRule(Of T, R As Csla.Validation.RuleArgs)(handler As Csla.Validation.RuleHandler(Of T, R), args As R)': Type parameter 'T' for 'Public Sub AddRule(Of T, R As Csla.Validation.RuleArgs)(handler As Csla.Validation.RuleHandler(Of T, R), args As R)' cannot be inferred.
    'Public Sub AddRule(handler As Csla.Validation.RuleHandler, args As Csla.Validation.RuleArgs)': Method 'Public Function KeyNotZero(target As Object, e As InsiteLib.CustomValidation.RuleArgs) As Boolean' does not have the same signature as delegate 'Delegate Function RuleHandler(target As Object, e As Csla.Validation.RuleArgs) As Boolean'.
    'Public Sub AddRule(handler As Csla.Validation.RuleHandler, args As Csla.Validation.RuleArgs)': Value of type 'InsiteLib.CustomValidation.KeyNotZeroRuleArgs' cannot be converted to 'Csla.Validation.RuleArgs'.
    'Public Sub AddRule(Of T)(handler As Csla.Validation.RuleHandler(Of T, Csla.Validation.RuleArgs), propertyName As String)': Type parameter 'T' for 'Public Sub AddRule(Of T)(handler As Csla.Validation.RuleHandler(Of T, Csla.Validation.RuleArgs), propertyName As String)' cannot be inferred.
    'Public Sub AddRule(handler As Csla.Validation.RuleHandler, propertyName As String)': Method 'Public Function KeyNotZero(target As Object, e As InsiteLib.CustomValidation.RuleArgs) As Boolean' does not have the same signature as delegate 'Delegate Function RuleHandler(target As Object, e As Csla.Validation.RuleArgs) As Boolean'.
    'Public Sub AddRule(handler As Csla.Validation.RuleHandler, propertyName As String)': Value of type 'InsiteLib.CustomValidation.KeyNotZeroRuleArgs' cannot be converted to 'String'. C:\Inetpub\wwwroot\Insite\InsiteLib\InsiteLib\Q1.designer.vb 1908 9 InsiteLib

I call using:

ValidationRules.AddRule(AddressOf CustomValidation.KeyNotZero, New CustomValidation.KeyNotZeroRuleArgs("InWork", 0))

Must be doing something wrong

Richard

 

 

Copyright (c) Marimer LLC