XAF Application Development tutorial (5) verification module, xaf Application Development
Data Verification is the most frequently used function module in application development. This section describes how to use the verification module in XAF.
The XAF verification module has the following built-in verification rules:
Verification rule type |
Description |
RuleCombinationOfPropertiesIsUnique (RuleCombinationOfPropertiesIsUniqueAttribute) |
The attribute values must be unique when they are combined. |
RuleCriteria (RuleCriteriaAttribute) |
The specified conditions must be met. For example, you must enter one for a landline or mobile phone. Condition: the agent is not null or the mobile phone is not null. |
RuleFromBoolProperty (RuleFromBoolPropertyAttribute) |
The attribute value of the specified boolean type must be true. For example, you must select and agree to a series of terms when registering a user. |
RuleIsReferenced (ruleisreferencedattriced) |
Must be referenced. |
RuleObjectExists (ruleobjectexistsattriists) |
The object must exist. |
RuleRange (RuleRangeAttribute) |
The value must be within a certain range. |
RuleRegularExpression (RuleRegularExpressionAttribute) |
The input value must meet the regular expression. |
RuleRequiredField (RuleRequiredFieldAttribute) |
The required value is required. |
RuleStringComparison (RuleStringComparisonAttribute) |
The string must meet certain comparison conditions. |
RuleValueComparison (RuleValueComparisonAttribute) |
The required value must meet certain comparison conditions. |
RuleUniqueValue (RuleUniqueValueAttribute) |
The value must be unique. |
For a more intuitive understanding, let's start with the previous one. Start with required verification:
As shown in, an error message is displayed after you press save, save close, and save New button. A red error icon is displayed in front of the Name text box. How can this effect be achieved?
public class customer ...
{
..........
private string _name;
[RuleRequiredField (CustomMessageTemplate = "Please fill in name!")]
public string name
{
get {return _name;}
set {SetPropertyValue ("name", ref _name, value);}
}
.........
In the project I used earlier, I opened the customer .cs file and added a line to the name attribute of the customer class
[RuleRequiredField (CustomMessageTemplate = "Please fill in name!")]
Yes, only one line is needed.
In this way, XAF generates a validation rule for us. The name of the rule is automatically specified, and a series of related ones are also used with default values. Let's modify the code and specify the name:
[RuleRequiredField ("Customer name is required", DefaultContexts.Save, CustomMessageTemplate = "Please fill in name!")]
The first parameter is the name of the rule. DefaultContexts.Save means that validation is performed only when this context is saved.
After compiling, open the xafml file, you can see all the information of such rules:
Follow the path in the red box on the left and finally click on the node "Customer name is required". The details of this rule appear on the right. The settings in the format group are all message types. Can be Chinese.
Those with the earth icon need to be localized.
Behavior column:
InvertResult: Invert the result. When set to True, that is, if the result of the verification fails, it is considered to pass, that is, whether the result of the pass is reversed. This option is not useful for required verification, and we cannot perform a verification. For a rule to implement certain properties it must not be filled.
ResultType: Error, Warning, Information, of which:
Error: When the verification rules are not met, an error is reported, and the operation cannot be continued. The validation rules demonstrated above have this effect.
Warning: When the verification rules are not met, a warning message is displayed. After confirmation, the program continues to run.
For example, there is an address attribute in the customer information. Of course, we want to fill in the address. If it is not filled in, when there is a market event, we will not be able to send some gifts to customers. At this time, we can add a rule like this, but if you ca n’t find the address, you ca n’t fill it in, but you need to remind the operator.
When you press Save, you can see a yellow exclamation mark icon in front of the address text box. At the position of the error message, a checkbox appears with the text Ignore warnings. When the checkbox is selected and you press Save again, this message is no longer displayed.
Let's take a look at the effect of changing the ResultType to Information:
Code:
private string _address;
[RuleRequiredField (ResultType = ValidationResultType.Warning, CustomMessageTemplate = "If you do not fill in the address, the marketing gift will not be delivered. Are you sure not to fill it?")]
public string address {
get {return _address;}
set {SetPropertyValue ("address", ref _address, value);}
}
Because the name is not filled in, the required rules are triggered and the address information is displayed, but it can be corrected without displaying the information.
When the name is filled in, the address message will not be displayed. At this time, clicking the Validation button will see a prompt message:
private string _address;
[RuleRequiredField (ResultType = ValidationResultType.Information, CustomMessageTemplate = "If you do not fill in the address, the marketing gift will not be delivered. Are you sure not to fill it?")]
public string address {
get {return _address;}
set {SetPropertyValue ("address", ref _address, value);}
}
Next, look at the SkipNullOrEmptyValues of the rules in xafml. This option is meaningless when required for verification, but it is meaningful in other rules, for example: the address must start with "Shanghai", if it is not filled, it will not be verified, then This value can be set to True.
Code:
private string _address;
[RuleRequiredField (ResultType = ValidationResultType.Information, CustomMessageTemplate = "If you do not fill in the address, the marketing gift will not be delivered. Are you sure not to fill it?")]
[RuleStringComparison ("Address must start in Shanghai", DefaultContexts.Save, StringComparisonType.StartsWith, "Shanghai", SkipNullOrEmptyValues = true)]
public string address {
get {return _address;}
set {SetPropertyValue ("address", ref _address, value);}
}
If the address is not filled in, the rules will not take effect. The save is passed directly, which is what SkipNullOrEmptyValues does.
TargetContextIDs: This attribute is a string of characters and can be filled in at will. Let's first look at what is Context, that is, context:
Context refers to an environment that enables validation rules to take effect, such as when saving data, when reviewing data, when deleting data, when storing data, etc., but the system actually only provides two default context environments, which we used earlier
DefaultContexts.Save this enum, you can see its definition (I use Reflector to view):
[Flags]
public enum DefaultContexts
{
Delete = 2,
Save = 1
}
You can see that this enum is Flags, that is, multiple values can be used simultaneously. Only delete and save are defined.
There are only two types of contexts built into the system. Of course, we can define our own contexts, please see the custom context in the next section.
We can see that there are a lot of attributes in the rules of xafml. In fact, these attributes can be controlled in the code. When writing the attribute, we can use the intelligent perception of VS to see what parameters can be set. In contrast to XAFML Information, you can know the function.