Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Centralize your validation error messages in a thin business rules document

0.00/5 (No votes)
18 Dec 2007 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here