SetEntityFieldError / Collection -> Has Errors Test?

Posts   
 
    
ianvink
User
Posts: 393
Joined: 15-Dec-2006
# Posted on: 17-Jul-2007 15:50:35   

A user enters bad record into a datagrid and I set the Field Error via entity.SetEntityFieldError()

When the User chooses the Save() option, I need to determine if the collection has any entities with these errors.

Is there some type of text I can call on the Collection which will indicate this?

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 17-Jul-2007 17:27:54   

Hi,

I'm not sure if that answer will fit your needs but you can get errors for a field with that code :

((IDataErrorInfo)entity)["foobar"]
ianvink
User
Posts: 393
Joined: 15-Dec-2006
# Posted on: 17-Jul-2007 17:40:40   

I'm looking for more of a

MyCollection.HasErrors()

Method.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-Jul-2007 18:13:50   

Errors in entities are kept inside these entities. So you should traverse the entities in the collection and check their IDataErrorInfo properties. This is because the collection is just a collection, not the central storage point of an entity. This means that bookkeeping about contents of entities is done inside entities, not inside a collection (as entities can be in multiple collections or not in a collection at all simple_smile )

Frans Bouma | Lead developer LLBLGen Pro
ianvink
User
Posts: 393
Joined: 15-Dec-2006
# Posted on: 19-Jul-2007 21:19:18   

I wrote the following function to determine if an Entity has errors.

The part where I test a field for IDataErrorInfo is always null. Is there a more correct way to test if a field has IDataErrorInfo info?

public class EntityHasError { public static bool HasErrors(EntityBase Entity) { IDataErrorInfo err = Entity as IDataErrorInfo; if (err != null) if (err.Error != "") return true;

        for (int x = 0; x < Entity.Fields.Count - 1; x++)
        {

** IDataErrorInfo field = Entity.Fields[x] as IDataErrorInfo; if (field != null) if(field.Error!="") return true;** }

        return false;
    }

}

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 20-Jul-2007 09:43:28   

IDataErrorInfo is implemented in the EntityBase(2) only, not on the EntityField(2) class. Try the following code.

public class EntityHasError
{
        public static bool HasErrors(EntityBase Entity)
        {
            IDataErrorInfo err = Entity as IDataErrorInfo;
            if (err == null)
                        return false;
            if (err.Error != "")
                    return true;

            for (int x = 0; x < Entity.Fields.Count - 1; x++)
            {
                string fieldError = err[Entity.Fields[x].Name];
                if(fieldError != "")
                        return true;
            }

            return false;
        }
}