Newbie Question - Validate that at least 1 child row exists

Newbie Question - Validate that at least 1 child row exists

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


RichP posted on Tuesday, December 30, 2008

Hello,

Sorry if this the answer is painfully obvious, but I am pondering the best way for an Editable root object (with a BusinessListBase 'child' list property) to validate there is at least 1 entered/non-deleted child object on a Save. 

Do I override BusinessBase.Save() and do my own checking, or is there better and/or more elegant way to handle this?

Thanks in advance for any help.

Rich.

daniel_higgins replied on Tuesday, December 30, 2008

It would be helpful if you indicated why the pattern in the project tracker sample application does not work for you. The Project class demonstrates how a root object saves its children.

tetranz replied on Tuesday, December 30, 2008

I think it's best to use a validation rule so that your root object's IsValid flag and broken rules collection works as they should. A rule is usually applied to a property so your requirement of at least one child doesn't quite fit the normal pattern. I think at least one clean way to do it would be to create a boolean property of your root called something like "HasChildren" which returns the value of ChildList.Count > 0. Then you can create a simple rule that checks HasChildren and returns valid if HasChildren is true.

The only issue I can think of is deciding when to actually check that rule. You probably need to catch the ListChanged event of the list and do a ValidationRules.CheckRules("HasChildren").

I've done this sort of thing with a Windows forms UI and bound a checkbox of zero size (so it's invisible) in a convenient place on the form so that the data error icon appears when the rule is broken.

Ross

RichP replied on Wednesday, December 31, 2008

Thanks Ross, this is exactly what I was looking for...  Have a safe and happy new year!

Rich.

ajj3085 replied on Monday, January 05, 2009

If it's Csla 3.5 or higher, you should be able to do this by override OnChildChanged.

Copyright (c) Marimer LLC