Powered by SmartDoc

Validating input

You can validate input with using validateand passattributes for CKTextField and CKText. If the input pass validation, variable for passattribute is true, or false.

Example: validating mail addresses

For example, we validate a text field to input mail addresses. We need to write code for checking whether the mail addresses exist or not, but we can check format of them with validating input.

In this case, we check the addresses include at mark (@). If the addresses don?t include at mark, pass_mailvariable is true.

Mail : CKTextField {
  value    = mail
  validate = "mail =~ /[^@]+@(.+)/"
  pass     = pass_mail
}

This example is attached to archive as Registration application.

Format for rules

Examples of format are the following.

name == ?MyName?
(title =~ /R/) and (title.size > 10)
not (count < 20)

Format for rules is ?attribute operator value? (Attribute is accessor method or instance variable defined in component class). You can join rules with and/or, use notto deny rules. If you use the operators, enclose rules with parenthesis.

Converting data types

Last rule In the above example, set value as number. Form data is setted as string for the valule, but the data is converted temporarily when validating. For example, the following validates input as number whether greater than or equal to 100 and less than equal to 500.

Number : CKTextField {
  value    = number
  validate = ?(number >= 100) and (number <= 500)?
  pass     = pass_number
}

Operators

Operators can be included in rules are the following.

Operators in rules
Operator Description
== Both sides are equal.
!= Both sides are not equal.
> Left is greater than right.
< Left is less then right.
>= Left is greater than or equal to right.
<= Left is less than or equal to right.
=~ Pattern matching.