CSLA .NET

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

Required list

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

Top 500 Contributor
Posts 19
MTmace Posted: Mon, May 18 2009 6:45 PM

I have a list that is a property of my root class. I would like to make sure that at least one item exists in the list before it is saved. So I added this rule

public static bool ListRequired(object target, Csla.Validation.RuleArgs e)
{
System.Collections.IList value = (System.Collections.IList)Csla.Utilities.CallByName(target, e.PropertyName, Csla.CallType.Get);
if (value == null || value.Count == 0)
{
e.Description =
string.Format("{0} required", Csla.Validation.RuleArgs.GetPropertyName(e));
return false;
}
return true;
}

But my root object is still invalid when I add a record to the object.

How can I validate the collection when a item has been added to it?

Top 10 Contributor
Posts 6,265

Your parent object needs to handle the collection's ListChanged event and in that event handler call ValidationRules.CheckRules("ChildCollectionPropertyName")

Rocky

Not Ranked
Posts 6

Hello Rocky:

Where is the best place to add the addhandler for the ListChanged event if the collection is "lazy loaded" in the parent object?

Thank you very much.

Top 75 Contributor
Posts 97
xAvailx replied on Mon, Feb 8 2010 1:03 PM

One option is to override in your root class the ChildChanged event and then call the appropriate validation rule. The args will give you what object was changed.

Not Ranked
Posts 6

That's a good option.

Thank you very much.

Not Ranked
Posts 6

The last step is to notify the "list required" message to the user. I've set a propertyStatus binded to my child property collection. The problem is that propertyStatus does not show the error message. The child property collection is defined as readonly because it's only a reference to an editable list. so this property doesn't have a propertychanged. How can I get propertyStatus to show the error message?

Not Ranked
Posts 6

It seems that if I call PropertyHasChanged(ChildCollectionProperty.Name) instead of Validation.CheckRules(ChildCollectionProperty) in the ChildChanged event the propertyStatus does show the error message. Is this the way to accomplish this?

Top 10 Contributor
Posts 3,592
Andy replied on Wed, Feb 10 2010 7:56 AM

Well, ValidationRules.CheckRules runs the rule check; but for the UI to know a property has changed (and thus, to query whether or not an error indicator is needed), it must receive a PropertyChanged event for the relevent property.  The two are related in that ValidationRules.CheckRules will update the broken rules collection, which is what will supply the information about errors to the UI when asked.

Page 1 of 1 (8 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