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:

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:

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. +/- 231-1. To require that the age needs to be between 12 and 99:

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:
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:

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:

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:

matches(fieldName) { regular expression }
Regular expressions must be valid regu lar expressions in Java, for more details please refer to the specification and tutorial.

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

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:

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:

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:

Operator Meaning
== equal

= #

not equal
\< less than
\> greater than
=\< less than or equal
\>= greater than or equal

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

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:

compare(name) { lower(name) != name } : "Your name must not contain only lower-case letters!"
Two fields can be compared in a similar way
compare(birthdate, graduationDate) { birthdate < graduationDate } : "You cannot finish school before your birthdate!"

Multi-field uniqueness definition#

Similar to the unique field attribute, 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

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

Note

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, as well as from the field attributes, 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 section of the MDD documentation.

Category Documentation


There is an option to let you decide where the error message will appear: next to the field or on top of the page. It's the attribute "annotation" of the mak:form. According to the documentation, you need to use either "before" or "after" values to place the error before or after the input field, otherwise it'll appear on top of the page.

--Orel, 05-Jan-2010 23:18


Add Comment
« This page was last updated on May 16 2010