OnValidateFieldValue in ASP.NET

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 22-Oct-2008 20:15:38   

I'm writing a very simple POC app that uses LLBL objects which are consumed by a WinForm and Web version.

The WinForm (DeveloperExpress) correctly consumes the IDataError published in this code on the entity.


        protected override bool OnValidateFieldValue(int fieldIndex, object value)
        {
            bool toReturn = true;
            switch ((CustomerFieldIndex)fieldIndex)
            {
                case CustomerFieldIndex.Country:
                    if (value.ToString() != "USA" && this.User != null && this.User.Country == "USA")
                    {
                        SetEntityFieldError(CustomerFields.Country.Name, "USA users can only set country to USA", true);
                        toReturn = false;
                    }
                    else
                    {
                        SetEntityFieldError(CustomerFields.Country.Name, string.Empty, false);
                    }
                    break;
                default:
                    toReturn = true;
                    break;
            }
            return toReturn;
        }


In a general sense, how is this done on the web, does ASP.NET consume the IDataError info that SetEntityFieldError() publishes?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Oct-2008 05:50:42   

Hi Ian,

As far as I know, the IDataErrorInfo was designed for WinForms, that's why the WinForms controls consume very well that info. And that's why the ASP.Net validator controls don't.

What you can do for consumes the info in ASP.Net is this:

/// <summary>
/// Shows the entity errors to the user.
/// We use a BulletedList to list the errors.
/// </summary>
/// <param name="exceptionMessage">The exception message that contains the errors list.</param>
protected void ShowEntityErrors(string exceptionMessage)
{
    /// Get the error information via the exception message.
    /// In this case the CustomerValidator class return the errors attached to the exception message string 
    /// separated by a semicolon (;)

    // using a BulletedList to list the validation errors presented.
    blCustomerEntityErrors.Items.Clear();
    string[] errors = exceptionMessage.Split(new char[] { ';' });
    foreach (string error in errors)
    {
        if (error.Trim() != string.Empty)
        {
            blCustomerEntityErrors.Items.Add(error);
        }
    }

    // show the errors list to the user
    blCustomerEntityErrors.Visible = true;
}

I know, not ideal, but that's a workaround.

David Elizondo | LLBLGen Support Team