ValidationRules and UI

ValidationRules and UI

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


alainrodriguez posted on Wednesday, September 12, 2012

I am trying on the UI control event OnValidating(CancelEventArgs e), to recurse thru all the BrokenValidationRules and 'highlight' the controls requiring validation.

My concern is that I need a 'unique' way to identify each Rule to each corresponding control on the form. Using the BizObject.BrokenRulesCollection, except the Property and the OriginProperty is null since this is ValidationRule without a PrimaryProperty as:

 

BusinessRules.AddRule(new PropertyRequired());
...
 private class PropertyRequired : BusinessRule
    {
      protected override void Execute(RuleContext context)
      {
         ....
          context.AddErrorResult("The object name " + myobj.Name.toString() + " required a Property!");
      }
    }
...
Matching the description is not possible since it is a dynamic string, 
  and this is resolved only when the rule is broken.
What mechanism can I use for this?
Thanks!

 

 

JonnyBee replied on Thursday, September 13, 2012

I assume we are talking Windows Forms UI here?

First make sure that your rules is attached to a property. context.AddErrorResult has overloads that accept
a PropertyInfo to tell which property the message is related to.  An object level rule may add messages to
several properties (provided they are added to the AffectedProperties list in the constructor of the rule).

IE:  Context.AddErrorResult(<propertyInfo>, "The object name " + myobj.Name.toString() + " required a Property!");

Next - there is standard components that do this for you. Looke at the ErrorWarnInfoProvider in CslaContrib

I would also encourage you to use PropertyRules as much as  possible as these are automatically executed by
CSLA when you change a property.
ObjectLevel rules must be manually triggered by your code!!!

The OnValidating is probably not the correct event to hook into either. The rules is run (and possibly async) and may affect a number of properties when the value is written to the BO. That does not happen until after OnValidating.

You should rather hook into the OnPropertyChanged on the BO or use the CurrentItemChanged on the BindingSource.

 

 

alainrodriguez replied on Thursday, September 13, 2012

thanks for the prompt reply!, very useful! that does it :)

Copyright (c) Marimer LLC