How to use validation?

Topics: User Forum
Sep 23, 2007 at 8:17 PM
Edited Sep 23, 2007 at 8:18 PM
The documentation for validation is a bit unclear to me. If I set up a class like this:

DbTable("users")
public class User : DbObject
{
MaxLength(5)
public string first_name;
public User() {}
}


I can still save it even though the first_name property is too long:

User newUser = new User();
newUser.first_name = "A string longer than 5";
DbEntry.Save(newUser);

I receive no errors and the user is saved. How do I use the validation rules I specify for each property?

The validation control should be done inside class user on the Save event (to prevent it from being duplicated all over an application). And DbEntry.Save cannot be overridden?
Coordinator
Sep 24, 2007 at 2:53 AM
DbEntry doesn't call validation function in Save. It need manually call.

ValidateHandler vh = new ValidateHandler(true, 1);
if(!vh.ValidateObject(newUser))
{
    throw new Exception("validate failed");
}
And, the ValidateHandler owns error messages in it could be show for users.

If use DbObjectModel as BaseType, the code could be following:

if(newUser.IsValid())
{
    throw new Exception("validate failed");
}
The DbEntry.Save cannot be override, and I don't think it could be.

In current release, the object.Save cannot be override too, but it already changed. It's not release, but you can get the source code first.