SetEntityFieldError

Posts   
 
    
loren
User
Posts: 14
Joined: 29-Jul-2006
# Posted on: 25-Aug-2006 18:05:46   

I'm using SetEntityError and SetEntityFieldError to set the IDataErrorInfo interface values in the OnValidateFieldValue method.

((IDataErrorInfo)this).Error and ((IDataErrorInfo)this)["CatalogSection"] are both set correctly when OnValidateFieldValue exits, however, when control returns to SetNewFieldValue (in the generated code), ((IDataErrorInfo)this)["CatalogSection"] is an empty string, but ((IDataErrorInfo)this).Error still contains the value set in OnValidateFieldValue. I would expect both values to have remained as set.


        protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            if (((ProductFieldIndex)fieldIndex == ProductFieldIndex.CatalogSection) && (value.ToString().Trim().Length == 0))
            {
                SetEntityFieldError(((ProductFieldIndex)fieldIndex).ToString(),
                    "Catalog Section cannot be blank.",
                    false);
                SetEntityError(((ProductFieldIndex)fieldIndex).ToString() + " cannot be blank.");
                this.
            }
            return true;
        }

It looks like the field value for IDataErrorInfo is being reset in the SetNewFieldValue method of EntityBase after OnValidateFieldValue is invoked.

Is this the expected behavior? Should I implement this another way? Perhaps by overriding the OnFieldValueChanged or in the EntityContentsChanged event handler?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 25-Aug-2006 19:33:13   

Hi,

Generally speaking, you should not override that method but instead use the existing IValidator feature. You may regenerate your projects having the validator classes generation task checked in your scenario, and modify the corresponding validator to your needs. Then use the corresponding constructor with the entity to get validated.

Now about your IDataErrorInfo problem, it may be something different. I'll have a look in the source code.

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 25-Aug-2006 19:49:13   

Ok got it.

Your IDataErrorInfo gets indeed reset in one of the following methods accessed (SetNewFieldValue or SyncFKFields) but that's because you're returning true, thus validating and allowing the saving.

If you don't want to validate the field, then you should return false, the saving process won't get processed and your errors should remain.

Again you should do that in a proper validator rather than in your override. More generally, there are very few situation when you should consider overriding the core methods. This because as illustrated here, you probably will get problems correctly handling the complex entities' life cycle.

There are quite often features available to hand, and if you find something really annoying, consider posting there. Frans will be able to tell you what's involved and what you can do.

Cheers

loren
User
Posts: 14
Joined: 29-Jul-2006
# Posted on: 27-Aug-2006 04:14:48   

Thanks. I regnerated the project with the Validator classes and things are working.

BTW, I was using the OnValidateFieldValue override because I found that as a sample in the documentation under "Generated code - Validation per field or per entity"... Please consider modifications to that portion of the document.

Thanks for your help.