Newsletter

Struts2 Validation Rules and Their Syntax

Struts » on Oct 24, 2011 { 2 Comments } By Sivateja

For every rule there is a class, all classes here are implements Validator interface

  • required
  • requiredstring
  • stringlength
  • int
  • double
  • date
  • email
  • 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

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, here we cannot pass any parameters
Example:

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

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

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

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

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

<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

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

2 Responses to “Struts2 Validation Rules and Their Syntax”
  1. pramod says:

    How I can find Action class object in servlet

  2. Vijaya says:

    Hi,
    I am trying to use “fieldexpression” tag as you mentioned. I am working on Struts 2.0.11.

    // validator name: fieldexpression
    if (form.elements[‘endDateString’]) {
    field = form.elements[‘endDateString’];
    var error = “Your Birth date must be less then your father dob”;
    }

    I am getting but the If condition is not added.
    I have added below statement in XML file

    <![CDATA[#endDateString
    Your Birth date must be less then your father dob

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.