You can validate input with using validate
and pass
attributes for CKTextField and CKText. If the input pass validation, variable for pass
attribute is true, or false
.
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_mail
variable is true
.
Mail : CKTextField { value = mail validate = "mail =~ /[^@]+@(.+)/" pass = pass_mail }
This example is attached to archive as Registration application.
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 not
to deny rules. If you use the operators, enclose rules with parenthesis.
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 can be included in rules are the following.
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. |