Asp.net| Package | data
Our business system involves more forms to edit and verify, the simplest way is to use the ASP.net data validation control, but this has the following 3 questions:
1 Check control to add to the form at design time, and manually set: data type, error information and other attributes, more cumbersome.
2 for an input box, we may want to check multiple items, such as: required, must be date format, and more than 2007-1-1. This makes it necessary to add multiple validation controls.
3 Business rules and forms are bound together and are not conducive to maintenance and reuse.
The verification features we want are:
1 validation rules and form separation, the form of the input box can be drawn, the validation control at run time, according to the configuration file dynamic creation.
2 according to the validation rules, generate the default: cannot be null, wrong number type, must be between 1 to 100 such a hint information.
3 Flexible Validation Rule expressions: Range (1), >=0.5, Mail (), Mobile (). Not currently
Conditions such as and, or, but easy to expand.
For this we encapsulate this part of the function, when used as long as:
Set in XML file: Control name, data type, whether required, validation expression information.
<ValidateInfo>
<ControlName> Outlet Quantity </ControlName>
<DataType>Integer</DataType>
<Require>true</Require>
<expression>range (1) </Expression>
</ValidateInfo>
In the page Page_Init event, call the class method:
Validatehelper.loadfromfile ("Data validation _ Configuration. xml").
Class Design:
Configuration Information Class
Class Validateinfo
{
public string controlname;
Public ValidationDataType DataType;
public string errormessage;
public bool Require;
public string Expression;
}
Validation control Create factory
public class Validatorfactory
{
Createrequiredfieldvalidator ();
Createdatetypecheckvalidator ();
Createrangevalidator (string min, string max);
Createcomparevalidator (ValidationCompareOperator oper, string valuetocompare);
...
}
Validation rule parser, creating a validation control based on configuration information
public class Validatorparser
{
Parse (validateinfo info, validatorfactory factory);
...
}
Read the configuration information and bind the validation controls to the form
public class Validatehelper
{
LoadFromFile (Page page, string fileName)
}
The attachment is sample code and documentation: Click here to download the source file