Validation rules and custom descriptions (error messages)

Validation rules and custom descriptions (error messages)

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


rrahlf posted on Thursday, March 29, 2007

I'm hoping that someone will tell me that I'm just missing something here.  The common validation rules provide no way to customize the error messages (RuleArgs.Description) aside from changing the Csla Resources files...is that correct?

I want to display the business object's validation errors directly on my web form, but the built in messages are somewhat unfriendly.  In addition to that, they can be confusing to a user - for example when using a drop-down list, the first item in the list can be left blank to indicate no selection has been made.  If the user is required to make a selection from the list, and the list values are integers representing IDs of the items in the list, then using the IntegerMinValue rule seems appropriate (first item selected is invalid).  However, if the list contains colors (with numeric ID's as the values of the drop down list) then the error message "0 is less than 1" can be pretty confusing. 

Another issue is reusability.  If I want to reuse my IntegerMinValue rule in many places, on many different drop-downs lets say, then I may want a different error in each case, even if the validation logic is the same.  "Please pick a color" and "Please select a beverage" for example, but in both cases the rule is just checking that the index is greater than 0.

I understand that I can create any number of validation rules that I would like, both instance and static, but it completely eliminates reusability if I have to duplicate validation logic just to change the error message. 

I'd much rather be able to pick an error message from my application's resource files and set it to the RuleArg.Description property when I add the rule to the object.  That almost works now, except that the CommonRules methods all overwrite the Description property of the RuleArgs when the rule returns false - regardless of whether or not that property is already set.

Am I way off?  What am I missing?  Do I really have to create a new IntegerMinValue type of rule with the same rule logic every time I want to change the error message for a specific object?  What is the cost of creating the rules the way I'm suggesting?

Thanks for reading.  Who has some ideas?

ajj3085 replied on Friday, March 30, 2007

I think the only way to handle this would be to create your own MinValue method that accepts a descrption to use if the rule is broken... possibly via a subclass of RuleArgs? 

Your rule would cast to the new subclass, do the check then set the description to the one passed via rule args.

I'm not sure how well that would work... this is just off the top of my head.

Andy

JoeFallon1 replied on Friday, March 30, 2007

This topic has been discussed a lot in the past so a search should help a lot. Also, there may be examples of what I am talking about below.

Bottom line is that you did not miss anything and many other people feel the same way.

For CSLA 2.x I decided to include a layer of classes between CSLA and my BOs. This has proven very valuable as I no longer have to modify the framework directly.

I created my own library of Common Rules (I copied Rocky's rules for starters and then only use my own.) My library has extra parameters so you can pass a friendly name for the property. Rocky plans to add this into the next version if he has time. My RuleArgs class also has more constructors so you can write 1 line rules instead of creating the instance and setting some properties. This helps with codegen.

Joe

 

rrahlf replied on Friday, March 30, 2007

Thanks for the posts!  Now I know I wasn't missing anything and can get back to work.

Copyright (c) Marimer LLC