problem with validation

problem with validation

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


vbdotnetguy posted on Friday, December 08, 2006

Hello all!  I recently downloaded and implemented the 2.1 version and now I'm getting the following exception

DataPortal.Fetch failed (Csla.DataPortalException: DataPortal.Fetch failed (System.InvalidOperationException: Invalid rule method (instance methods of the target object not allowed)
   at Csla.Validation.ValidationRules.ValidateHandler(MethodInfo method)

and here is my business rules.  am i missing something?

Protected Overrides Sub AddInstanceBusinessRules()
        ValidationRules.AddInstanceRule(AddressOf HasVacationDays1, "Code1")
        ValidationRules.AddInstanceRule(AddressOf HasVacationDays1, "Hours1")

        ValidationRules.AddInstanceRule(AddressOf HasVacationDays2, "Code2")
        ValidationRules.AddInstanceRule(AddressOf HasVacationDays2, "Hours2")

        ValidationRules.AddInstanceRule(AddressOf HasVacationDays3, "Code3")
        ValidationRules.AddInstanceRule(AddressOf HasVacationDays3, "Hours3")
    End Sub

    Private Function HasVacationDays1(ByVal target As Object, ByVal e As Validation.RuleArgs) As Boolean
        If mEmployeeID > 0 Then
            If mCode1.ToLower = "e4" Then
                Return HasVacationDays(mHours1, e)
            End If
        End If

        Return True
    End Function

    Private Function HasVacationDays2(ByVal target As Object, ByVal e As Validation.RuleArgs) As Boolean
        If mEmployeeID > 0 Then
            If mCode2.ToLower = "e4" Then
                Return HasVacationDays(mHours2, e)
            End If
        End If

        Return True
    End Function

    Private Function HasVacationDays3(ByVal target As Object, ByVal e As Validation.RuleArgs) As Boolean
        If mEmployeeID > 0 Then
            If mCode3.ToLower = "e4" Then
                Return HasVacationDays(mHours3, e)
            End If
        End If

        Return True
    End Function

    Private Function HasVacationDays(ByVal HoursTaking As Double, ByVal e As Validation.RuleArgs) As Boolean
        Dim emp As Employee = Employee.GetEmployee(mEmployeeID)
        Dim days As Double = UsedVacationList.GetUsedVacationDays(mEmployeeID, Date.Today.Year.ToString)
        Dim Taking As Double = HoursTaking / 8
        Dim UsedTime As Double = days + Taking

        If Convert.ToDouble(emp.VacationDays) < days Then
            e.Description = "Employee does not have anymore available vacation"
            Return False
        ElseIf Convert.ToDouble(emp.VacationDays) < UsedTime Then
            e.Description = String.Format("The employee only has a total of {0:#.#} days of vacation left for this year which is a total of {1} hours", Convert.ToDouble(emp.VacationDays) - days, (Convert.ToDouble(emp.VacationDays) - days) * 8)
            Return False
        End If

        Return True
    End Function

vbdotnetguy replied on Friday, December 08, 2006

nevermind i figured it out

jules140 replied on Thursday, December 14, 2006

I'm having the same problem. Could you let me know how you solved it?

Cheers

xal replied on Thursday, December 14, 2006

ValidateHandler is only called when you do:
ValidationRules.AddRule, not when you call ValidationRules.AddInstanceRule.

I don't see why it would be called in your code, since you are using instance rules...

The exception is raised when adding a type rule (via AddRule, not AddInstanceRule) and the method isn't static an is declared inside your BO class.

So if the method is not static, but lives in an external object (like a validation object), or if the method is static you shouldn't have any issues.

What's your scenario??

Andrés

jules140 replied on Tuesday, January 02, 2007

Thanks guys,

AddInstanceRule works a treat.

Happy New Year to all.

Jules

Copyright (c) Marimer LLC