Validation Rule Basics


Working Example
We will use a simple example to demonstrate the key concepts used in Envalidate of rules, conditions, comparisons and values.

Say we have an Account class which has a Balance property. We want to make sure that the balance is never less than 0, and never more than 5000.
To do this we would create a Rule named "Account Balance". We would then sets its ValidCondition specifying the restrictions we want to place on the account balance.

Account.Balance >= 0 AND Account.Balance <= 5000.

Conditions and Comparisons
You can see that this Condition is made up of 2 comparisons. A Condition is made up of 1 or more Comparisons that are combined with the logical operators AND, OR, and NOT.

A Comparison returns either true or false by comparing values.

Values
Our example has 3 values. Account.Balance, which is used twice, 0, and 5000. Account.Balance comes from the data being validated, while 0 and 5000 are pre-defined in the rule. Values can either be elements of the data being validated, or pre-defined literals. Values can also be combined using arithmetic operators (addition, subtraction etc.) or in various other ways based on their type. For example Account.Balance + 1000, which is an addition, or Account.Name + " is overdrawn", which is a string concatenation.

Rules
Validation Rules are made up of 2 conditions, the Active Condition and the Valid Condition. The Active Condition determines when the rule is active. If this condition evaluates to false, the rule is not active and so is always considered to be valid.

If the Active Condition evaluates to true, the Valid Condition will also be evaluated. If it evaluates to true, the data is valid. If on the other hand it evaluates to false, then the data is invalid because it breaks this rule.

Rule Message
Each rule has a Value called broken message. Because this is a value just like the ones used in the conditions, you can use elements from the data in the message that is related to the rule when it is broken. When a rule is evaluated as being broken, the Message is automatically evaluated and the resulting string placed in the MessageText property. This makes it easier to display the messages of any broken rules to the user.