Validators: Into the Deep
(Page 1 of 4 )
In the first part, we saw a glimpse of what the Validator framework can do. But that is just the tip of the iceberg. The driving force behind creation of the Validator framework is reusability. This facet of the framework is but one of the many aspects that can not only reduce the web GUI development time but also enforce standards across all interfaces.
In this part I shall take you into the depth of Validator framework so that by the end, resolving all the major validation cases will be "easy like the wind." The first section will deal with the types of Validators, and in the second section all of these Validators will be put to use in validating inputs of the first point in user management -- the "Registration page."
So let’s start by examining the various types of validations provided by the framework.
Types of Validators
The Validators, or the Standard rules, which is the other term by which they are also known, come in varieties. They are:
- Required Validator
- Data Type Validator
- Range Validator
- Email Validator
- Mask Validator
- Date Validator
- Length Validator
Let's take a look at them one by one.
Required Validator
This rule is used to validate mandatory fields. It is the commonly used validation. The fields where this rule is applicable are user id, password, address, and so on. For example, if the name of the field is “Password” then the required rule would be:
<field property="name" depends="required">
<arg0 key="registration.Name"/>
</field>
Data Type Validator
There are occasions when the data should be of a specific type. The best example is the field specifying age. It must be numeric. So the data type Validator can be applied. This rule can validate six types of data:
- byte
- short
- integer
- float
- long
- double
These correspond to the basic Java data types. What the rule checks is whether the input data could be converted to the data type provided as a part of the rule. The example of the age field would provide a better illustration.
<field property="age" depends="byte">
<arg0 key="registration.age"/>
</field>
Age can (almost) never cross 100. So the data entered must be less than or equal to one hundred. Hence the byte rule will fail. To validate the other data types, replace byte with the corresponding above mentioned type as called for by the value.
Range Validator
The range Validator does what the name suggests. If the rule is to make sure that the data entered is between specific ranges, then Range Validators are the rules to be applied. Before version 1.2, the Validator framework contained only the “range” rule. Today, to use the range Validator, one has to specify one of the following Validators/rules:
- intRange
- longRange
- floatRange
- doubleRange
These Validators correspond directly with the data type. That is, intRange is used for checking whether the input is an integer in the valid range. The same is true with the other range Validators. To cite a quick example, if the data to be validated is age (again), then the rule would look like this:
<field property="age" depends="required,integer,intRange">
<arg0 key="registration.age"/>
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>
<var><var-name>min</var-name><var-value>12</var-value></var>
<var><var-name>max</var-name><var-value>100</var-value></var>
</field>
When compared with other Validators, the range Validator is different. It uses elements/tags <arg1> and <arg2> to specify the minimum and maximum values for the range. The value provided as the key is used in <var> to provide the value for valid minimum and maximum values. Here valid values are between 12 and 100. Hence the <var> tags are used to specify the value. I will explain more about <var> later when I discuss variable and constants.
Next: Email, Mask, Date, and Length Validators >>
More JavaScript Articles
More By A.P.Rajshekhar