Validation rules on child collection

Validation rules on child collection

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


Frédéric posted on Tuesday, October 21, 2008

I have the following issue with validation rules. I have created a validation rule in one of my BO that will validate the state of one of the property of my child BO.

 

For example, let say I have Libray object which have a collection of Books object. I want to create a validation rule that validate that I don’t have in my collection a book of type “boring”. (note that a book by himself can be boring, so the validation rule must be at the library level)

 

ValidationRules.AddRule(Of Library)(AddressOf NoBoringBook, "Books")

 

 

Private Shared Function NoBoringBook (Of T As Library)( _

                ByVal target As T, _

                ByVal e As Validation.RuleArgs) As Boolean

 

        For Each aBook As Book In target.Books

            If aBook.IsBoring = True Then

 

     e.Description = "Cannot add boring books to my library"

            'Set the severity of the error, only Error severity breaks a rule

            e.Severity = Validation.RuleSeverity.Error

            'Stop checking other rules

            e.StopProcessing = True

 

            End If

        Next

 

    End Function

 

 

 

My problem is that the validation is not fired if I add a boring book and save my library. My understanding is that the validationrule.checkrule will run if the PropertyAsChange is set to true on a property. But in my case, books is not a property but a collection of child object.

 

Did someone already had this issue and is there an easy solution. Thanks. 

 

Fred

skagen00 replied on Tuesday, October 21, 2008

In prior versions of CSLA one would subscribe to the ListChanged event of the collection from the root. Then, in the handler for the ListChanged, you would call ValidationRules.CheckRules("Books"). The subscription should happen both when the object is initially instantiated as well as within an OnDeserialized override.

In the newest versions of CSLA there is an OnChildChanged event that you may override in your root object. If the e.ChildObject is the collection or it "is a book", then make the call to ValidationRules.CheckRules("Books"). ListChanged subscriptions won't work the same way in the newest versions of CSLA, you need to use OnChildChanged.

Hope that helps!

 

JoeFallon1 replied on Tuesday, October 21, 2008

Another idea is to override IsValid in your parent object and make the call there.

ValidationRules.CheckRules("Books").

Then call MyBase.IsValid.

Joe

 

skagen00 replied on Tuesday, October 21, 2008

Ah, didn't think of that. While your BrokenRulesCollection won't necessarily be accurate through the lifetime of the object up until saving, there are few places where I recollect actually examining the BrokenRulesCollection for an object -- and the overhead is less as you wouldn't be responding to changes in the list all the time to calculate the rule some of the time.

And I suppose if it's important for the BrokenRulesCollection to be meaningfully accurate you could simply override BrokenRulesCollection and do the same sort of thing.

Just trying to think if there are any other side effects by "deferring" the checking of the rule until you request IsValid...

 

Copyright (c) Marimer LLC