Entity Validation, unable to callout the error thrown

Posts   
 
    
yj
User
Posts: 39
Joined: 07-Aug-2007
# Posted on: 08-Aug-2007 11:43:33   

Hi guys, today i tried to write my partial classes to do the validation on the fields and entity level. I am facing some problem with the following code that i had written (i m using self-servicing):

protected override void OnValidateEntityBeforeSave() { EmployeeEntity toValidate = new EmployeeEntity();

        if (this.Name  == null )
        {
            throw new ORMEntityValidationException("Name cannot be null", toValidate);
        }
        if (this.Address  == null)
        {
            throw new ORMEntityValidationException("Address cannot be null", toValidate);
        }
        base.OnValidateEntityBeforeSave();
    }   

I wrote the following code to try out the overriden function above: EmployeeEntity employee = new EmployeeEntity(); employee.EmpId = 19093; employee.ValidateEntity(); I did not set any value for Name and Address for the employee entity, however there is not error thrown when i execute the ValidateEntity() method. Am i missing out something? Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 08-Aug-2007 11:49:29   

OnValidateEntityBeforeSave is called right before the entity is actually saved. You call ValidateEntity, which is a general validation routine. These are separated, so you can do different validations before a save and when you call ValidateEntity(). If you want response from ValidateEntity() I'd suggest either to override ValidateEntity() or add a validator object and in that validator object implement ValidateEntity.

If you save the entity you wrote, it will give the errors you expected.

Frans Bouma | Lead developer LLBLGen Pro
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 08-Aug-2007 14:48:22   

also you don't need to declare a new instance of the entity. just pass the current entity to the excpetion.

protected override void OnValidateEntityBeforeSave()
{
    if (this.Name == null )
    {
        throw new ORMEntityValidationException("Name cannot be null", this);
    }
    if (this.Address == null)
    {
        throw new ORMEntityValidationException("Address cannot be null", this);
    }
    base.OnValidateEntityBeforeSave();
}