Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET
Article

Centralize your validation error messages in a thin business rules document

Rate me:
Please Sign up or sign in to vote.
1.67/5 (12 votes)
18 Dec 2007CPOL1 min read 24.8K   60   15   4
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:

XML
<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 = "*".

C#
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:

C#
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)


Written By
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 Pin
Lubna_0424-Mar-10 16:44
Lubna_0424-Mar-10 16:44 
Could you please explain UIValidators as I could not find the definition of it in your sample code
GeneralUsing the Code Pin
Dewey4-Nov-07 20:35
Dewey4-Nov-07 20:35 
GeneralRe: Using the Code Pin
Viswanath Majeti16-Dec-07 20:17
Viswanath Majeti16-Dec-07 20:17 
AnswerRe: Using the Code Pin
Viswanath Majeti18-Dec-07 22:03
Viswanath Majeti18-Dec-07 22:03 

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

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