Our business system involves a lot of form editing and verification. The simplest way is to use the data verification control that comes with asp.net, but this has the following three problems:
1. The verification control must be included in the design Adding it to the form and manually setting attributes such as data type, error message, etc. is quite cumbersome.
2 For an input box, we may have to check multiple items, such as: required, must be in date format, and must be greater than 2007-1-1. This requires adding multiple validation controls.
3 Business rules and forms are bound together, which is not conducive to maintenance and reuse.
The verification functions we hope for are:
1. The verification rules are separated from the form. Just draw the input box on the form. The verification control will be dynamically created according to the configuration file at runtime.
2 According to the verification rules, a default prompt message is generated: cannot be empty, wrong number type, must be between 1 and 100.
3 Flexible validation rule expressions: such as: range(1, 200), >=0.5, Mail(), Mobile(). Currently, conditions such as and, or are not
supported
, but they can be easily extended.For this reason, we have encapsulated this part of the function. When using it, you only need to
set the following in the xml file: control name, data type, whether it is required, and verification expression information.
Here is a quote:
<ValidateInfo>
<ControlName>Quantity of outlets</ControlName>
<DataType>Integer</DataType>
<Require>true</Require>
<Expression>range(1, 100)</Expression>
</ValidateInfo>
In the Page_Init event of the page, call the class method:
ValidateHelper.LoadFromFile("data verification_configuration.xml") can be used.
Class design:
//Configuration information class
classValidateInfo
{
public string ControlName;
public ValidationDataType DataType;
public string ErrorMessage;
public bool Require;
public string Expression;
}
//Validation control creation factory
public class ValidatorFactory
{
CreateRequiredFieldValidator();
CreateDateTypeCheckValidator();
CreateRangeValidator(string min, string max);
CreateCompareValidator(ValidationCompareOperator oper, string valueToCompare);
...
}
// Verification rule parser, creates verification controls based on configuration information
public class ValidatorParser
{
Parse(ValidateInfo info, ValidatorFactory factory);
...
}
// Read the configuration information and bind the verification control to the form
public class ValidateHelper
{
LoadFromFile(Page page, string fileName)
}