CSLA .NET

From Rockford Lhotka's Expert C# 2008 and VB 2008 Business Objects books

Newbie Question - Validate that at least 1 child row exists

rated by 0 users
This post has 4 Replies | 1 Follower

Not Ranked
Posts 2
RichP Posted: Tue, Dec 30 2008 7:24 PM
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.
Top 500 Contributor
Posts 27

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.

Top 25 Contributor
Posts 225
tetranz replied on Tue, Dec 30 2008 9:01 PM
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
Not Ranked
Posts 2
RichP replied on Wed, Dec 31 2008 11:13 AM
Thanks Ross, this is exactly what I was looking for...  Have a safe and happy new year!

Rich.
Top 10 Contributor
Posts 3,715
Andy replied on Mon, Jan 5 2009 10:55 AM
If it's Csla 3.5 or higher, you should be able to do this by override OnChildChanged.
Page 1 of 1 (5 items) | RSS

Please contact Magenic for your .NET consulting
and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems