[{TableOfContents}]\\

!!!Overview
Validation rules are defined inside a data definition, and allow Makumba to automatically validated form data input.

The general syntax of a validation rule definition is as follows:
[{Code

ruleType(arg1, arg2, ...) { rule definition } : "error message"

}]

!!!Validation rule types

There are different kind of validation rules, each with a specific name:

* number range (''range'')
* string length (''length'')
* regular expression (''matches'')
* comparison (''compare'')
* uniqueness (''unique'')

The arguments depend on the type, as we'll see later.

!!Number range rule

The range definition rule applies for {{Number}} types, i.e. {{int}} and {{real}}. The validation rule is defined as:
[{Code

range(fieldName) { 1..10 }
}]        

Using ? allows to keep one parameter unbound, which will default to java.lang.Integer.MIN_VALUE and java.lang.Integer.MAX_VALUE, respectively, i.e. +/- 2%%sup 31/%-1.
    
To require that the age needs to be between 12 and 99:
[{Code

range(age) { 12..99 } : "Age must be between 12 and 99!"
}]
To define that we need to be at least 5 year old, but without any upper limit:
[{Code

range(age) {5..? } : "Age must be at least 5!"
}]

!!String length rule

The length definition rule is very similar to the range definition, but applies for {{string}} types, i.e. {{char}} and {{text}}. The validation rule is defined as:
[{Code

length(fieldName) { 1..10 }
}]        
Using ? allows to keep one parameter unbound, which will default to a minimum length of 0 and a maxium length of 255

To define that the CV needs to be at least 100 characters, define:
[{Code

length(cv) { 100..? } : "CV must be of decent length!"
}]
    
!!Regular expression rule

The string regular expression rule applies for {{string}} types, primarily for {{char}}, but also for {{text}}. The validation rule is defined as:
[{Code

matches(fieldName) { regular expression }
}]
Regular expressions must be valid regu    lar expressions in Java, for more details please refer to the [specification|http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html] and [tutorial|http://java.sun.com/docs/books/tutorial/essential/regex/].

To compare if a field contains a valid e-mail address, define:
[{Code

matches(email) { .+@.+\.[a-z\.]+ } : "Email address is not valid!"
}]

!!Comparison rule

This rule allows to compare a field to a constant value or simple function calls.

The following constants are currently available:
[{Table

||Constants 	||Description 	||Valid
|$now 	|The current date and time. 	|date types
|$today 	|The current date, time set to 00:00.|^
}]

The following functions are currently available:
[{Table

||Function 	||Description 	||Valid
|$date(.., .., .., .., .., ..)
|Constructs a date.\\
The function takes up to six arguments, for day, month, year, hours, minutes and seconds. If less than six arguments are passed, the missing values will be automatically set to the current date.\\
$now is available as a constant for the current date value, and + and - operators can be used to modify that value. e.g. {{date($now,$now,$now - 5)}} evaluates as the current date 5 years ago.
|{{date}} types

|lower(..) |Converts a string to lower case. |{{String}}, i.e. 'char' and 'text' types
|upper(..) |Converts a string to upper case.|^
}]

Valid comparison operators are the ones defined in Java:
[{Table

||Operator ||Meaning
|== |equal
|!= |not equal
|\< |less than
|\> |less than
|=\< |less than or equal
|\>= |greater than or equal

}]

To compare if someone is at least 15 years old, define the following validation rule:

[{Code

compare(birthdate) { birthdate >= date($now,$now,$now - 15) } : "You have to be at least 15 years old!"
}]

To ensure that a field doesn't contain only lower-case values, define this rule:
[{Code

compare(name) { lower(name) != name } : "Your name must not contain only lower-case letters!"
}]
Two fields can be compared in a similar way
[{Code

compare(birthdate, graduationDate) { birthdate < graduationDate } : "You cannot finish school before your birthdate!"
}]

!!Multi-field uniqueness definition

Similar to the [unique field attribute|DataDefinitions#FieldAttributes], you can also define multi-field uniqueness contraints. These constraints can even span over several data definitions, if they are related via pointers.

Multi-field uniqueness can be defined as follows
[{Code

unique(age, email) : "the combination of age and email must be unique!"
}]

[{Box

If the fields are in the same data definition, the uniqueness definition will translate into a database level multi-field key and uniqueness will be enforced there.
Otherwise, Makumba will issue a query checking the uniqueness before new or edit operations.
}]

!!!Error message

The error message that will be displayed if the validation fails.


!!!Implicit validation rules
From the types defined in the [Data Definition|DataDefinitions], as well as from the [field attributes|DataDefinitions#FieldAttributes], Makumba can automatically infer validation rules as follows:

|| field type || field attribute || validation rule || default message
| int | | check for an integer number | invalid integer
| real | | check for a real number | invalid real
| boolean | | check for boolean value | invalid boolean
| | unique | | This field needs to be unique. Try another value 
| | not null | | A non-null value is required for this field 
| | not empty | | A non-empty value is required for this field 

The default messages can be redefined in the MDD, as detailed in the [field attributes section|DataDefinitions#FieldAttributes] section of the MDD documentation.


%%(display:none;)[Category Documentation]%%
