Field value validate

Posts   
 
    
dotnethao
User
Posts: 10
Joined: 27-Jul-2006
# Posted on: 30-Jul-2006 17:05:59   

Hi,

I add following code in my project to validate:


    public partial class EmployeesEntity : EmployeesEntityBase
    {
        protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            bool toReturn = true;
            switch ((EmployeesFieldIndex)fieldIndex)
            {
                case EmployeesFieldIndex.BirthDate:
                    // birthday is valid if employee is adult.
                    if (value != null)
                    {
                        if (((DateTime)value).AddYears(18).CompareTo(DateTime.Now) > 0)
                            toReturn = false;
                    }
                    else
                    {
                        toReturn = true;
                    }
                    break;
                case EmployeesFieldIndex.HireDate:
                    if (value != null)
                    {
                        toReturn = ((DateTime)value).CompareTo(DateTime.Now) > 0;
                    }
                    else
                    {
                        toReturn = true;
                    }
                    break;
                default:
                    toReturn = true;
                    break;
            }
            return toReturn;
        }
    }

I want to cancel saving while validation failed. It can debug into above code while saving, but the action(save) did not cancel(Data saved into database).

How can i get the validate status while validation failed? Thanks.

sparmar2000 avatar
Posts: 341
Joined: 30-Nov-2003
# Posted on: 30-Jul-2006 17:33:09   

Hello

I think the section on 'Validation' in version 2.0 manual should help you resolve this issue. Please feel freeto come back if you stil have a question.

dotnethao
User
Posts: 10
Joined: 27-Jul-2006
# Posted on: 30-Jul-2006 18:01:50   

sparmar2000 wrote:

Hello

I think the section on 'Validation' in version 2.0 manual should help you resolve this issue. Please feel freeto come back if you stil have a question.

Hello,

Can you give me some example code about this( How to implement)?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 31-Jul-2006 07:24:14   

OnValidateFieldValue is used to validate setting a new value to an entity's field in memory.

What you need to use is OnValidateEntityBeforeSave