Required list

Required list

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


MTmace posted on Monday, May 18, 2009

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?

RockfordLhotka replied on Monday, May 18, 2009

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

OscarLauroba replied on Monday, February 08, 2010

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.

xAvailx replied on Monday, February 08, 2010

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.

OscarLauroba replied on Tuesday, February 09, 2010

That's a good option.

Thank you very much.

OscarLauroba replied on Wednesday, February 10, 2010

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?

OscarLauroba replied on Wednesday, February 10, 2010

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?

ajj3085 replied on Wednesday, February 10, 2010

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.

Copyright (c) Marimer LLC