将您的验证错误消息集中在一个精简的业务规则文档中






1.67/5 (8投票s)
如何将验证错误消息集中到精简的业务规则文档中。
引言
过去几年,我作为项目负责人的一项工作职责是执行代码审查并强制执行所有使用 .NET 技术团队开发的 Web 项目的编码规范。我们已经逐渐演进,以更快的速度完成项目,拥有更简洁的 UI 和更好的后端系统集成。其中一项工作就是将验证错误消息集中到精简的业务规则文档中。
背景
借助 VS 2005 提供的验证控件,在客户端提供验证是一个简单的拖放功能。一些属性可以在设计时分配,但我发现以运行时的方式分配 ErrorMessage
和 ValidationExpression
属性更方便。
Using the Code
下图显示了网页上带有必填字段验证器和正则表达式验证器的验证错误消息。
现在,让我们看一下提供验证消息和表达式的业务规则文档
<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>
现在,看看我们如何在 Page_Load
事件中关联这些消息。在设计时,将 ErrorMessage = "*"
和 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;
}
同样,我们可以在 GridView
的 RowDataBound
事件中将这些消息关联到验证控件,如下所示
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;
}
注意:示例网站是使用 MySQL 5.0 数据库构建的。提供程序库可在官方 MySQL 网站上免费下载。您需要创建一个包含表 AddressType [nAddressTypeID
, vAddressTypeName
, vAddressTypeCode
, dtCreatedDate
, vCreatedBy
, dtModifiedDate
, vModifiedBy
] 的数据库,并取消注释 DataAccessLayer
类(/DataAccessObjects/AddressType.cs)中引用 MySQL 库的代码。