How to validate whether a child item exists?

How to validate whether a child item exists?

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


raz0rf1sh posted on Thursday, January 29, 2009

I have an Invoice object with a collection of InvoiceItem objects.

I need to add a validation rule that says at least on InvoiceItem object is required. Where is it best to do this at?

reagan123 replied on Thursday, January 29, 2009

***Edit *** Nevermind, I mis-read your post :(

Antonio Sandoval replied on Thursday, January 29, 2009

I have the same question.

I was doing it with overriden IsValid method. Chapter 8. Expert CSharp Bussines Objects:

public override bool IsValid(){
get{
   return base.IsValid && _ChildrenList.IsValid /* && _ChildrenList.Count>0 */;
}
}

But maybe a better way is using a custom Rule:

ValidationRules.AddRule<Root>(ChildrenAtLeastOne<Root>,"ChildrenList");

 private static bool ChildrenAtLeastOne<T>(T target, Csla.Validation.RuleArgs e) where T: class
{
            int count = ((Csla.Core.IExtendedBindingList)Csla.Utilities.CallByName(target, e.PropertyName, CallType.Get)).Count;
            return count > 0;
}

In the two cases I have the problem of how to automate the error notification in the user interface. Thanks in advance.

Wbmstrmjb replied on Thursday, January 29, 2009

Don't add it to IsValid().  Do your second suggestion by requiring at least one object via validation.  But the validation can go in the Child List, not the Root object.  Most likely that is a business rule for all instances of that list, so add it there instead.  HTH.

raz0rf1sh replied on Thursday, January 29, 2009

So ChildLists allow for validation rules now?

JoeFallon1 replied on Thursday, January 29, 2009

No. Child Lists do not have ValidationRules. I am not sure what the other poster was referring to.

The list can raise events that the root BO can listen to. Then the root BO can call CheckRules for the specific child collection property name. But the rule would need to exist in the root BO.

Joe

 

Antonio Sandoval replied on Thursday, January 29, 2009

That sounds logic, but BussinesListBase neither EditableRootListBase supports Rule Validation. The second method works, but I must use PropertyHasChanged("GetChildrensList") instead of OnPropertyChanged(null)  (ProjectTracker) in the ListChanged Event, that permits to CSLA verify the rules for this children list. But I'm still don't sure if it is a good way of do that.


ajj3085 replied on Friday, January 30, 2009

Your Invoice should be sub-classing BB though; I'd be really surprised if there were a use case that did it any other way.

Copyright (c) Marimer LLC