For every rule
there is a class, all classes here are implements
Validator interface
- required
- requiredstring
- stringlength
- int
- double
- date
- url
- fieldexpression
- regex
- few other rules are there but light..!!
For each rule given, there is an implementation class exist for it as told above.
Example:
For ‘required’ rule the class name is RequiredFieldValidator, for ‘int’ the class name is IntRangeFieldValidator
For ‘required’ rule the class name is RequiredFieldValidator, for ‘int’ the class name is IntRangeFieldValidator
If we want to create our own validator class then our class should also
implements validator interface.
Validator interface is given in com.opensymphony.xwork2.validators.*.
required
This rule
verify whether user entered value is null or
not, hear we cannot pass any parameters
Example:
Example:
|
1
2
3
4
5
|
<field name="uname">
<field-validator type="required">
<message>
User name is mandatory </message>
</field-validator>
</field>
|
requiredstring
This will validates whether the input entered is a valid
string or not we can pass a parameter called trim
to this rule.
Trim will removes extra space from
left and right sides of given word
Example:
Example:
|
1
2
3
4
5
6
|
<field name="uname">
<field-validator type="requiredstring">
<param
name="trim">true</param>
<message> you must enter string value </message>
</field-validator>
</field>
|
stringlength
This rule
validates whether the given input is with in
the given range of characters or not. This rule is the combination of
both min, max
length rules.
To this rule
we can pass either both parameters min length
and max length or any one of the paramenters
Example:
|
1
2
3
4
5
6
7
|
<field name="uname">
<field-validator type="requiredstring">
<param
name="minLength">8</param>
<param
name="maxLength">12</param>
<message>
user name should be with in ${minLength} and ${maxLength} characters </message>
</field-validator>
</field>
|
int
This rule verifies whether the given
input value is with in the given range of integer or not, To this rule we need to pass min, max
parameters
Example:
|
1
2
3
4
5
6
7
|
<field name="uname">
<field-validator type="int">
<param
name="min">25</param>
<param
name="max">35</param>
<message>Age
should be in between ${min} and ${max} </message>
</field-validator>
</field>
|
Note: Actually ‘double‘ also
same as int, but we need to pass the parameters minInclusive,maxInclusive
date
This will validate whether the given
input date is with in the range of the dates
or not, we need to pass min, max parameters to this rule.
When we enter the date values then
the format to be followed is dd-mm-yyyy
Example:
|
1
2
3
4
5
6
7
|
<field name="dob">
<field-validator type="date">
<param
name="min">01/01/2011</param>
<param
name="max">01/01/2020</param>
<message>year
should be in between ${min} and ${max} </message>
</field-validator>
</field>
|
And email,
url are simple just like required, we no
need to pass any parameters hope you are good.
fieldexpression
In struts 1.x, with validator frame work we can do only per field validations, i mean we can validate
field by field. But hear in struts 2,x we can do the between field validations also with this fieldexpression rule.
Actually if we want to do between
fields in struts 1.x, we must do manually but
hear we can do this via xwork validator framework
which supports both per-field and between filed validations also.
To work with this rule we must pass
one parameter named ‘expression‘
Between field means, we can compare the current entered value
with any one of above field.
Example:
|
1
2
3
4
5
6
7
8
9
10
11
|
<field name="myDateOfBirth">
<field-validator type="fieldexpression">
<param
name="expression">
<!
[ CDATA[#myDateOfBirth < #myFatherBirth]] >
</param>
<message>Your
Birth date must be less then your father dob</message>
</field-validator>
</field>
|
Note: here #myFatherBirth is
other field name
No comments:
Post a Comment