More newbie questions re validation.

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 18-Dec-2006 05:54:54   

Environment: LLBLGenPro Demo (latest version) Windows Forms .Net 2.0 SelfServicing Infragistics ultraGrid version 6.3

I have a grid and the data is validated each time the user moves to a new row. (see code below)


public partial class CurrencyEntity : EntityBase, ISerializable
    {

        protected override void OnValidateEntityBeforeSave()
        {
            base.OnValidateEntityBeforeSave();

            // Currency.Code Validations

            if (Code.Length == 2)
            {
                SetEntityFieldError("Code", string.Empty, false);
            }
            else
            {
                SetEntityFieldError("Code", "Code must be two characters in length", false);
            }

            // Currency.Description Validations

            if (Description.Length == 5)
            {
                SetEntityFieldError("Description", string.Empty, false);
            }
            else
            {
                SetEntityFieldError("Description", "Description must be five characters in length", false);
            }
        }


protected internal void ultraGrid1_AfterRowUpdate(object sender, RowEventArgs e)
        {
             // ultraGrid1BS is a BindingSource
             ((EntityClasses.CurrencyEntity)(ultraGrid1BS.Current)).Save();
        }

The databinding is setup a follows: CurrencyEntityCollection is the datasource for ultraGrid1BS --> BindingSource ultraGrid1BS is the datasource for ultraGrid1

When I run the code above the validations work I get the appropriate error messages associated with the correct rows and cells. However despite the data showing up as invalid it is being persisted to the database. I don't understand this because validation is supposed to execute prior to any entity.save() and so invalid changes should not be persisted?

Can you please advise what I might be doing wrong?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 18-Dec-2006 08:49:50   

There 2 kinds of validations:

Per Field: that's when you want to validate a value before setting it to a field, thus a field can not be set to an invalid value.

Per Entity: If you want to validate an Entity before or after certain events, like before you save the entity. Here if you have not used a ValidationPerField already, and it happens that you have an invalid value for a field (the invalid value is already set), then using SetEntityFieldError will only set the error message for that field, it won't prevent it from being persisted in the database.

Instead you should early enough use Filed Validation, Otherwise, you should either alter the field Value to be saved OR you should stop Save action (the entire save action of the entity) by thowing an ORMEntityValidationException.