Applying Validation Rules Sometimes

Applying Validation Rules Sometimes

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


ballistic posted on Thursday, January 11, 2007

Hello,

Working on a site where I require a username.  I need to make sure the username is unique.  So I am palnning on adding a rule to the SET in the property which checks to see that the username is indeed unique.

However, when I pull a member record from the database and set it's properties, I do not want to run this validation, which I think will run everytime I set it.  Is there a way to add a conditional so that I know if the properties' SET is being called with data from a text field rather than from a DB?

- Andy

xal replied on Thursday, January 11, 2007

You're supposed to load the object directly to the fields, not through the object's properties. That would add unnecesary overhead (auth, rules, etc). It may even crash if the current user is not allowed to set one of the properties for that object, which is a common scenario.
Suppose a user can read all of the props, but can only write to just a few. Loading the object would instantly crash for no good reason.
Also, some property changes may trigger other validations for other properties, so you may be validating the same thing more than once unnecesarily.

What most people do is set the fields directly and call ValidationRules.CheckRules() at the end of the process.

Andrés

ballistic replied on Thursday, January 11, 2007

Thank you.  That makes sense.

As you can tell, I'm just getting started and I figured we always used the public properties to enforce validation.

I appreciate your input!

Copyright (c) Marimer LLC