Dependent Property Validation

Dependent Property Validation

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


Q Johnson posted on Friday, May 13, 2011

I'm having trouble with something that should be simple, I think.

I have a property called PositionStatus that is not required.  Another property DaysWorkedGTZero is usually not required, either.  But if PositionStatus has a value of "07", DaysWorkedGTZero has to have one of its allowable values: either Y or N.

So I tried this for a rule method:

       Private Shared Function DaysWorkedForStatus(Of T As RptDetailRecord)(ByVal target As T, ByVal e As RuleArgs) As Boolean
         If target.ReadProperty(_positionStatusProperty) = "07" Then
            e.Description = "'Days Worked Greater Than 0' is required when Position Status is '07'"
            Return False
         Else
            Return True
         End If
      End Function

and then I call these methods in my AddBusinessMethods routine:

         ValidationRules.AddRule(Of RptDetailRecord)(DaysWorkedForStatus(Of RptDetailRecord), _daysWorkedGTZeroProperty)

         ValidationRules.AddRule(Of RptDetailRecord)(DaysWorkedForStatus(Of RptDetailRecord), _positionStatusProperty)


         ValidationRules.AddDependentProperty(_positionStatusProperty, _daysWorkedGTZeroProperty, True)

This seems a pretty faithful mimic of the code Rocky offers in ppg507 and 509.  But my compiler is complaining about the calls to AddRule for the custom Rule saying "argument not specified for parameter 'e' of Private Shared Function DaysWorkedForStatus....yada, yada - and the same for 'target'.

I must be missing something obvious here.  Can you steer me right?

Thanks and have a great weekend.

 

JonnyBee replied on Saturday, May 14, 2011

AddBusinessRules should be like this:

         ValidationRules.AddRule(Of RptDetailRecord)(DaysWorkedForStatus, _daysWorkedGTZeroProperty)
         ValidationRules.AddRule(Of RptDetailRecord)(DaysWorkedForStatus, _positionStatusProperty)


         ValidationRules.AddDependentProperty(_positionStatusProperty, _daysWorkedGTZeroProperty, True)

 

 

 

Q Johnson replied on Saturday, May 14, 2011

Thanks for the reply, Johnny.

I tried that and stil have the same four compiler errors (both calls to .AddRule complaining about missing target and e), I'm sorry to say.

And their appearance in the code window is still the same, too.  Maybe this is a clue you can use whose importance eludes me. AddRule has the red squiggly line underneath and the Issue it references is "Undeclared element".  DaysWorkdForStatus has the blue squiggly and its tip message is "argument not specified for parameter 'e'....yada, yada".  I believe these are offered by VS 2008, but they may be provided by CodeRush.

It's curious to me that the 'target' argument gets to show the message for the missing 'e', but no such message shows for the propertyinfo argument.  Is that significant?

Thanks again.

JonnyBee replied on Saturday, May 14, 2011

Which version of Csla are you using?

I believe you ar missing the AddressOf keyword for the static method: :

Code taken from ProjectTrackervb;

    ValidationRules.AddRule(Of Project)(AddressOf StartDateGTEndDate(Of Project), StartedProperty)
    ValidationRules.AddRule(Of Project)(AddressOf StartDateGTEndDate(Of Project), EndedProperty)

    ValidationRules.AddDependentProperty(StartedProperty, EndedProperty, True)

  End Sub

  Private Shared Function StartDateGTEndDate(Of T As Project)( _
    ByVal target As T, _
    ByVal e As Validation.RuleArgs) As Boolean

    If target.GetProperty(Of SmartDate)(StartedProperty) > target.GetProperty(Of SmartDate)(EndedProperty) Then
      e.Description = "Start date can't be after end date"
      Return False

    Else
      Return True
    End If

  End Function

Q Johnson replied on Sunday, May 15, 2011

I'm using CSLA 3.8.4.

And adding the AddressOf did the trick! 

I had suspected that it might be the issue, having looked at some code in other posts.  But when I added it my original code here (that had the bogus (of RptDetailRecord), it didn't correct it, so I discarded it and posted my question here without it.

So your first suggestion would have fixed it if I'd just left it in!

Many years ago when the book for CSLA 1.x was published, Rocky made an errata list available on his site.  Do you know where there might be one for the current book?  I can't find one.  And I'd think the ommission of AddressOf on the code example in chapter 17 might qualify for it.

Thanks again, Jonny!

RockfordLhotka replied on Sunday, May 15, 2011

Apress tracks all errata for their books.

Copyright (c) Marimer LLC