Invalidating object if collection count is zero

Invalidating object if collection count is zero

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


timchandler posted on Monday, January 04, 2010

I'm looking at a way to invalidate an object if the collection count is zero.

Let's say I have a class Foo, with two properties. Name, which is a string, and MyItems, which is a collection of child items.

What I would like to do is have an error display on the Name Textbox (I'm using silverlight, fyi), indicating that the object is invalid until the user adds a MyItem.

So I have created a custom error routine as such:
private static bool OneOrMoreMyItemsRequired(T target, RuleArgs e) where T : Foo
{
if (target.ReadProperty(MyItems).Count == 0)
{
e.Description = "A Foo Object must have at least one MyItems";
return false;
}

return true;
}

In the override of AddBusinessRules, I add the following:
ValidationRules.AddRule(OneOrMoreMyItemsRequired, NameProperty, 2);

The priority of the rule is 2 because I have a name required rule I want to display first.

Everything seems to work as expected. I get a nice validation display error that I need at least one MyItems. However, when I add the MyItem, the Name property validation error doesn't update. If though I go into the Name textbox and change the name slightly, it does update and my errors go away.

Is there a way to get this message to disappear when I add the MyItem?

Is there a better way to do this?

Thanks in advance!
Tim

RockfordLhotka replied on Monday, January 04, 2010

Generally your thinking is correct. As you note, the trick is to trigger the rules when the collection changes. The easiest way to do this is to override the OnChildChanged() method - something like this:

    protected override void OnChildChanged(Csla.Core.ChildChangedEventArgs e)
    {
      base.OnChildChanged(e);
      ValidationRules.CheckRules(NameProperty);
    }

timchandler replied on Monday, January 04, 2010

Thanks for the reply.

Yeah, I was thinking that would work too. But for some reason it doesn't seem to like that.

Embarassingly, I tried the following and it did work.

protected override void OnChildChanged(Csla.Core.ChildChangedEventArgs e)
{
base.OnChildChanged(e);
base.OnPropertyChanged("IsDirty");

// TODO: Fix hack with better code.
string tmpName = Name;
Name = String.Empty;
Name = tmpName;

}

As you can see, I'm not particularly proud of this solution. But it seems like I need to actually change the text of the silverlight textbox in order for the error message to clear.

Any thoughts on that?

McManus replied on Monday, January 04, 2010

RockfordLhotka:

Generally your thinking is correct. As you note, the trick is to trigger the rules when the collection changes. The easiest way to do this is to override the OnChildChanged() method - something like this:


protected override void OnChildChanged(Csla.Core.ChildChangedEventArgs e)
{
base.OnChildChanged(e);
ValidationRules.CheckRules(NameProperty);
}



Probably you should call OnPropertyChanged("Name") after the checking the rules. This way, databinding is notified that it needs to update the binding to the Name property.

HTH,
Herman

timchandler replied on Monday, January 04, 2010

Yeah, I initially coded the OnPropertyChanged("Name") event, but that didn't seem to have any effect.

What else would be going on besides that event when i change the value using the assignment to and from the tempoary variable? I figure something else is going on behind the scenes? Or maybe some specific event isn't firing if I do an OnPropertyChanged event if the text of the old and new values match?

Tim

ajj3085 replied on Tuesday, January 05, 2010

Have you tried using the IdentityConverter on the binding?

Copyright (c) Marimer LLC