string.Empty satisfies Csla.Validation.CommonRules.StringRequired

string.Empty satisfies Csla.Validation.CommonRules.StringRequired

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


Voss posted on Sunday, January 28, 2007

Hey Everyone!

First off, Rockford you are a champ, I'm enjoying using your framework!

I couldn't help but think it a little strange though that my StringRequired business rule was not being broken by my objects' member being set to string.Empty; thus allowing my object to be saved with an empty field. My only consolation prize is that the second time around my Exists business rule wouldn't allow the object to be saved with the member equal to string.Empty again, because it was already in the database!

I had set the member variable equal to string.Empty in the DataPortal_Create() method (an old habit of always initalizing variables). Obviously the quick fix is to not initialize the variable, so that it is equal to null and breaks the StringRequired business rule, but I can't but feel strange about the C# language having string.Empty, and the object validation process evaluating it as a valid string against a rule that specifies that a string is required, allowing the object to be saved. I am using csla20cs-2.1-RTM061003, if this problem has been addressed in the latest version, then please disregard this, if not, then it's food for thought.

Cheers for your attention Peoples!!!

Voss

 

 

 

RockfordLhotka replied on Monday, January 29, 2007

I hate to say it, but I suggest you check your code. I just checked the history on CommonRules.cs, and this method hasn't changed for a very long time. And all my tests and sample app use string.Empty. You must initialize the field value to string.Empty or Windows Forms data binding will fail, so ProjectTracker and my other tests all do exactly that.

Also, if you look at the rule method code, it does check for both null and string.Empty - either one will cause failure. I'm using string.IsNullOrEmpty(), so if that's failing then .NET has a serious issue Wink [;)]

All that said, if you can provide an isolated example where it fails, I'm happy to fix a bug in the rule method.

McManus replied on Monday, January 29, 2007

Hi Voss,

Are you sure that the StringRequired rule is actually called? I suspect that you have to call ValidationRules.CheckRules() in your DataPortal_Create() method.

Cheers,

Herman

Voss replied on Tuesday, January 30, 2007

Hi Rockford & Herman!

Thank you both for replying. It turns out you are both right; Rockford your faith in your product led me to dig deeper for answers (which turn out to be on page 424 of your fantastic book!), Herman, you twigged my curiousity about what assumptions I had made about my code.

I was operating under the assumption that ValidationRules.CheckRules was automatically called on a business object at creation, which is why I was confused when testing my application to find I could save the object without editing any fields. My new assumption is that all objects base.IsValid property is set to true at object creation, regardless of what the members of the object are set to, and require as stated by you both, to have ValidationRules.CheckRules() called at DataPortal_Create.

Embarrassed [:$] Guess who feels like a silly newbie?

Thank you both again for your time and patience!

Voss

Copyright (c) Marimer LLC