ValidationRules

ValidationRules

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


sungeek posted on Wednesday, February 04, 2009

Has anyone come up with the need to seperate validation rules by type?  Let me explain, I have a very common scenario - a user BO that, on insert, needs to check one set of rules (like username exists or email address exists) but on update you do not want to check these rules.  So, what is the separation mechanism?

ajj3085 replied on Wednesday, February 04, 2009

The solution I usually use is to make the rule smart enough to know when it should apply and when it wouldn't.. so basically just checking IsNew and if it's false, let the rule run, otherwise return true.

sungeek replied on Wednesday, February 04, 2009

Wow - that is simple enough - thanks

rsbaker0 replied on Wednesday, February 04, 2009

Also, rules are normally checked only if you either explicitly call ValidationRules.CheckRules(), or if the property associate with the rule changes.

In the case of an update type operation, I don't  call CheckRules() on an object loaded from the database, so the only rules associated with the changes are called.

It's possible to be very creative, though. You can associate rules with non-existent property names, so they are never automatically called. If you want to call them yourself, you can call CheckRules just for that "pseudo" property. Some people here have used this to do server-side specific processing if I recall correctly.

Copyright (c) Marimer LLC