Validation Problem

Validation Problem

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


Bonio posted on Friday, July 28, 2006

I have an Item BO - it is comprised of three classes: ItemSKU (Editable Object), ItemSplits (a child list) and ItemSplit (an editable child).

The problem is this: the ItemSKU object's validation of its own variables works fine, however, validation of the ItemSplit object does not appear to occur. I have added the validation rules to the child object, but it just returns IsValid=true even when it is obviously not.

I appreciate that this is a tricky question without an example of the relevant code, but I was wondering whether anyone else had had this experience and may be kind enough to give me some tips.

Thanks

 

Bonio

Dawn replied on Friday, July 28, 2006

Do you override ItemSKU (Editable Object)'s IsValid like this.

public override bool IsValid
{
    get
    {
       return base.IsValid && ItemSplits (a child list).IsValid
    }
}

Dawn

Bonio replied on Friday, July 28, 2006

Hi Dawn

I am using this in my ItemSKU class:

public override bool IsValid

{

get { return base.IsValid && _splits.IsValid; }

}

For whatever reason the ItemSKU validation is working, but not validation for ItemSplits??

Thanks

Bonio

Bonio replied on Friday, July 28, 2006

Okay, I think I have found the problem Surprise [:O]:

It the set property of some of my string variables, I have the following:

set

{

CanWriteProperty("Description", true);

if (value == null) value = string.Empty;

if (!_description.Equals(value))

{

_description = value;

PropertyHasChanged("Description");

}

The reason for the problem was I was initializing my variable '_description' to be 'String.Empty', when a user fails to enter a value in 'Description' it remains as 'String.Empty' and therefore the 'PropertyHasChanged' method is not called. Any ideas how I should get round this? I tried not initializing to 'String.Empty' but had problems.

Thanks

Bonio

Bonio replied on Saturday, July 29, 2006

Having done a bit more research, I have fixed the problem by putting

Validation.CheckRules() in the child's constructor. Is the correct way?

Thanks

Bonio

xal replied on Saturday, July 29, 2006

If you call DataPortal.Create to make an instance of your child, then you probably want to do it inside DataPortal_Create.

The reason is that generally, some business objects also need to validate data when you Fetch it too, so you would call CheckRules() at the end of the Fetch method. In that situation, you would be calling CheckRules in the constructor and also inside fetch, which is not ideal.

If your fetch method doesn't do more than initializing defaults and checking rules, you can mark it with a RunLocal() Attribute, so that it doesn't go through remoting / enterprise services / whatever to do the initialization.

Another possibility is doing it inside your factory method, before returning the instance of your object.

Andrés

Copyright (c) Marimer LLC