Click here to Skip to main content
Click here to Skip to main content

Centralize your validation error messages in a thin business rules document

, 18 Dec 2007
Rate this:
Please Sign up or sign in to vote.
How to centralize your validation error messages in a thin business rules document.

Introduction

One of my job responsibilities as a Project Lead in the past few years has been to perform code review and enforce coding guidelines for all web projects developed in our .NET technologies team. We have slowly evolved to complete our projects faster, with cleaner UI and better integration with our back-office systems. One such effort was with centralizing validation error messages in a thin business rules document.

Background

With the validation controls that are provided in VS 2005, providing validation on the client side is an easy drag-n-drop feature. Some of the properties can be assigned at design-time, but I found assigning ErrorMessage and ValidationExpression properties at runtime more convenient.

Using the Code

The following two figures show validation error messages on a web page with a required field validator and a regular expression validator.

Screenshot - RequiredFieldValidators.jpg

Screenshot - RegularExpressionValidators.jpg

Now, let's take a look at the business rules document that provides the validation messages and expressions:

<BusinessRules>
 <AddressType> 
  <AddressTypeName maxLength="100" required="true"> 
    <ErrorMessage> 
      <TextBoxRequiredFieldValidation message="AddressType Name cannot 
                be a blank value" /> 
      <DropDownListRequiredFieldValidation message="Please select an 
                AddressType Name to continue" /> 
      <RegularExpressionValidation message="AddressType Name can accept only 
                letters" expression="[a-z A-Z]+" /> 
    </ErrorMessage>
  </AddressTypeName> 
  <AddressTypeCode maxLength="10" required="true"> 
   <ErrorMessage> 
     <RequiredFieldValidation message="AddressType Code cannot be a blank value" /> 
     <RegularExpressionValidation message="AddressType Code can accept only 
                letters" expression="[A-Z]+" /> 
   </ErrorMessage> 
  </AddressTypeCode> 
 </AddressType> 
</BusinessRules>

Now, look at how we can tie these messages in our Page_Load event. At design-time, set ErrorMessage = "*" and Text = "*".

StringBuilder columnPath = new StringBuilder();
XmlNode node = null;
if (rfvAddressTypeName.ErrorMessage.Length == 1) 
{ 
    columnPath.Append("BusinessRules/AddressType/AddressTypeName/" +
    "ErrorMessage/TextBoxRequiredFieldValidation"); 
    node = businessRules.SelectSingleNode(columnPath.ToString()); 
    rfvAddressTypeName.ErrorMessage = node.Attributes["message"].Value; 
    columnPath = null; 
    node = null; 
} 
if (revAddressTypeName.ErrorMessage.Length == 1) 
{ 
    columnPath = new StringBuilder(); 
    columnPath.Append("BusinessRules/AddressType/AddressTypeName/" +
    "ErrorMessage/RegularExpressionValidation"); 
    node = businessRules.SelectSingleNode(columnPath.ToString()); 
    revAddressTypeName.ErrorMessage = node.Attributes["message"].Value; 
    revAddressTypeName.ValidationExpression = 
        node.Attributes["expression"].Value; 
    columnPath = null; 
    node = null; 
}

Then again, we can tie these messages to the validation controls in the GridView's RowDataBound event, as shown below:

try 
{ 
    columnPath = new StringBuilder(); 
    columnPath.Append("BusinessRules/AddressType/AddressTypeCode/" +
    "ErrorMessage/RequiredFieldValidation"); 
    node = businessRules.SelectSingleNode(columnPath.ToString()); (
       (RequiredFieldValidator)e.Row.Cells[4].Controls[3]).ErrorMessage = 
       node.Attributes["message"].Value; 
} 
catch 
{
} 
finally 
{ 
    columnPath = null; 
    node = null; 
} 

try 
{ 
    columnPath = new StringBuilder();
    columnPath.Append("BusinessRules/" + "AddressType/AddressTypeCode" + 
                      "/ErrorMessage/RegularExpressionValidation"); 
    node = businessRules.SelectSingleNode(columnPath.ToString());
    ((RegularExpressionValidator)e.Row.Cells[4].Controls[5]).ErrorMessage = 
                                 node.Attributes["message"].Value;
    ((RegularExpressionValidator)e.Row.Cells[4].Controls[5]).ValidationExpression = 
                                 node.Attributes["expression"].Value; 
}
catch 
{ 
} 
finally 
{ 
    columnPath = null; 
    node = null; 
}

Note: The sample website was built using a MySQL 5.0 database. The provider library is available as a free download at the official MySQL website. You need to create a database with a table, AddressType [nAddressTypeID, vAddressTypeName, vAddressTypeCode, dtCreatedDate, vCreatedBy, dtModifiedDate, vModifiedBy] and uncomment code that references the MySQL library in the DataAccessLayer class (/DataAccessObjects/AddressType.cs).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Viswanath Majeti
Web Developer
India India
Viswanath Majeti works as a Project Lead in .NET Technologies for a software development company in Hyderabad, India

Comments and Discussions

 
GeneralUIValidators PinmemberLubna_0424-Mar-10 16:44 
GeneralUsing the Code PinmemberDewey4-Nov-07 20:35 
GeneralRe: Using the Code PinmemberViswanath Majeti16-Dec-07 20:17 
AnswerRe: Using the Code PinmemberViswanath Majeti18-Dec-07 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 19 Dec 2007
Article Copyright 2007 by Viswanath Majeti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid