Validation of child collection?

Validation of child collection?

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


jas_nhs posted on Tuesday, September 05, 2006

Validation rules:
I have successfully implemented some custom rules, so know how that can be used. However, my object (UserAccount) has a collection of Roles as a property, named UserAccountRoles (simple parent-child, and not related to Membership per se), and I was wondering if it is possible to devise a rule which checks that at least one Role has been assigned. I tried this:
    ValidationRules.AddRule(AccountRoleRequired,"UserAccountRoles");

    private bool AccountRoleRequired(object target, Csla.Validation.RuleArgs e)
    {
      if (_userAccountRoles.Count == 0)
      {
        e.Description = "User has not been allocated an Account Role";
        return false;
      }
      //else
        return true;
    }

but this gets called at object declaration time and the rule basically always fails.
Probably I am approaching this from the wrong angle - but how might I acheive what I want?

Thanks

James.



JoeFallon1 replied on Tuesday, September 05, 2006

You don't say what you mean by fails:

"this gets called at object declaration time and the rule basically always fails."

It could be something as simple as using the Property instead of the member variable: UserAccountRoles

Then just be sure the Property uses the lazy loading style:

If _userAccountRoles Is Nothing Then

 _userAccountRoles = UserAccountRoles.GetRoles

End If

Return _userAccountRoles

Joe

Copyright (c) Marimer LLC