Validations in Struts2

Validations in Struts2

In struts2 we can define validation using xml file. In struts2 for validation rules has classes and all classes implements validator interface.

Struts Action 2 relies on a validation framework provided by XWork to enable the application of input validation rules to your Actions before they are executed. Struts2 Validation Framework allows us to separate the validation logic from actual Java/JSP code, where it can be reviewed and easily modified later.

Validation framework comes with set of useful routines to handle form validation automatically and it can handle both server side as well as client side form validation. If certain validation is not present, you can create your own validation logic by implementing java interface.

  • required
  • requiredstring
  • stringlength
  • int
  • double
  • date
  • email
  • url
  • fieldexpression
  • regex For each rule given, there is an implementation class exist for it as told above.


    Examples:
    required
    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.*. 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 trimto 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="true">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

    double
    Double field validator check whether given input is double or not .
    fieldName - The field name this validator is validating. Required if using in-Validator Syntax otherwise not required
    minInclusive - the minimum inclusive value in FloatValue format specified by Java language (if none is specified, it will be checked)
    maxInclusive - the maximum inclusive value in FloatValue format specified by Java language (if none is specified, it will be checked)
    minExclusive - the minimum exclusive value in FloatValue format specified by Java language (if none is specified, it will be checked)
    maxExclusive - the maximum exclusive value in FloatValue format specified by Java language (if none is specified, it will be checked)
    Example:
    <validators>
    <!-- Plain Validator Syntax -->
    <validator type="double">
    <param name="fieldName">percentage</param>
    <param name="minInclusive">20.1</param>
    <param name="maxInclusive">50.1</param>
    <message>Age needs to be between ${minInclusive} and MaxInclusive} (inclusive)</message>
    </validator>

    <!-- Field Validator Syntax -->
    <field name="percentage">
    <field-validator type="double">
    <param name="minExclusive">0.123</param>
    <param name="maxExclusive">99.98</param>
    <message>Percentage needs to be between ${minExclusive}
    ${maxExclusive} (exclusive)</message>
    </field-validator>
    </field>
    </validators>


    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 framework 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
    email
    This rule validates whether the given input in the correct format or not.
    Example:
    <field name="email">
    <field-validator type="requiredstring">
    <message key="errors.required" />
    </field-validator>
    <field-validator type="email">
    <message key="errors.invalid" />
    </field-validator>
    </field>

    url
    This rule validates whether the given input in the correct format or not.
    Example:
    <field name="fblink">
    <field-validator type="url">
    <message>Please enter a valid url</message>
    </field-validator>
    </field>
    regex
    Validates a string field using a regular expression.
    Example:
    <field name="userId">
    <field-validator type="requiredstring">
    <message>Employee Id is a mandatory field</message>
    </field-validator>
    <field-validator type="regex">
    <param name="expression">
    <![CDATA[([E][m][p][0-9][0-9][0-9])]]>
    </param>
    <message>Valid Employee Id required e.g. Emp001</message>
    </field-validator>
    </field>

  • 0 comments:

    Post a Comment