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)How can I validate the collection when a item has been added to it?
Your parent object needs to handle the collection's ListChanged event and in that event handler call ValidationRules.CheckRules("ChildCollectionPropertyName")
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.
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.
That's a good option.
Thank you very much.
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?
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?
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