How to implement warning messages in CSLA

How to implement warning messages in CSLA

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


San posted on Wednesday, July 13, 2011

Hi, All

We are using CSLA 3.5 and we would like to implement some warning messages on our web app, i have no clue how we will do this in CSLA, could any one guide me through this please or share some code examples.

Thanks!

San

JonnyBee replied on Friday, July 15, 2011

Warning messages are essentially broken rules with Severity = Warning.

They are part of the BrokenRules collection as normal Error rules.

How you visualize this to the user is up to you.There may be helper classes in Csla.Web but I'm not sure about this. Check the Csla.Web namespace.

 

 

 

San replied on Friday, July 15, 2011

Hi, Jonny

I appreciate your response!  but i am running in to the problems,  Here is the example i would like to implement an warning message.

 private static bool NameValidation<T>(T target, RuleArgs e) where T : Project

 {

           if (target.name== null)

           {

                e.Description = "Are you sure you want to continue with out a Name";

                return false;         

       }

return true;

    }

The above code is in my validation class, So when they tab out from the texbox i would like to throw an warning message as "Are you sure you want to continute with out a Name" not as pop-up though, probaly I use a label control to populate the warning message. 

JonnyBee replied on Friday, July 15, 2011

you should also set e.Severity to Warning before returning false.

By default the Severity is Error.

San replied on Friday, July 15, 2011

Yes, it is working but  i can still see the warning messages after saving the record in to the database.

-San

JonnyBee replied on Friday, July 15, 2011

Well, you may have hit one of the challenges with a rule engine.

Assuming that NameProperty is a managed field you should be able to ask the FieldManager if the NameProperty is dirty or not (ie: has the user changed the NameProperty) and  only do the validation if the field is Dirty.

Which also means that you should verify that the Save method calls

 

ajj3085 replied on Saturday, July 16, 2011

Caling MarkOld or CheckRules is NOT necessary in the usual use cases, Csla will do that for you after a successful save or when you change the value in Name, respectiely.

If the rule should only be active for new records, or modified records, the rule should check IsNew or IsDirty before running.  For example the OPs rule would be this if the rule only applied when new or dirty:

if ( (target.IsNew || target.IsDirty) && target.name== null ){

  e.Description = "Name required";

  e.Severity = RuleSeverity.Warning;

  return false;

}

Copyright (c) Marimer LLC