The integer range field validator in Struts is used to check if an integer field falls within a specified range. Like other Struts validators, there are two forms of using this validator:

    • XML: using type=”int” attribute in <validator> or <field-validator> elements.
    • Annotation: using @IntRangeFieldValidator annotation type before field’s setter method or action method (plain-validator).

 

1. Struts Integer Range Field Validator XML

Usage:

  • Field-validator syntax:
    <field name="fieldName">
    	<field-validator type="int">
    		<param name="param name">param value</param>
    		<message>validation error message</message>
    	</field-validator>
    </field>

     

  • Plain-validator syntax:
    <validator type="int">
    		<param name="param name">param value</param>
    		<message>validation error message</message>
    </validator> 

 

Parameters:

Parameter name

Required

Description

fieldName

 

Name of the field (required if using plain validator syntax).

min

No

The minimum value to check. It won’t be checked if omitted.

max

No

The maximum value to check. It won’t be checked if omitted.

parse

No

Boolean value. If set to true, evaluate the minExpressionand maxExpression to find min/max.

minExpression

No

Expression to calculate the minimum value. It won’t be evaluated if omitted.

maxExpression

No

Expression to calculate the maximum value. It won’t be evaluated if omitted.

 

Struts Integer Range Field XML Examples:

  • Field-validator example:
    <field name="myAge">
    	<field-validator type="int">
    		<param name="min">18</param>
    		<param name="max">25</param>
    		<message>Your age must be between ${min} and ${max}</message>
    	</field-validator>
    </field>
  • Omitting the minimum check:
    <field name="myAge">
    	<field-validator type="int">
    		<param name="max">40</param>
    		<message>Your age must below ${max}</message>
    	</field-validator>
    </field>
  • Using minExpression and maxExpression:
    <field name="myAge">
    	<field-validator type="int">
    		<param name="minExpression">${minValue}</param>
    		<param name="maxExpression">${maxValue}</param>
    		<message>Your age must be between ${min} and ${max}</message>
    	</field-validator>
    </field> 

    In this case, we have to implement two methods getMinValue() and getMaxValue() in the action class as follows:

    public int getMinValue() {
    	return 20;
    }
    
    public int getMaxValue() {
    	return 40;
    }

     

  • Plain-validator example:
    <validator type="int">
    	<param name="fieldName">myAge</param>
    	<param name="min">18</param>
    	<param name="max">25</param>
    	<message>Your age must be between ${min} and ${max}</message>
    </validator> 

 

2. Struts @IntRangeFieldValidator Annotation

Usage: Put the @IntRangeFieldValidatorannotation before the setter method or action method (in case of using plain-validator) in the following form:

@IntRangeFieldValidator(param1 = "param 1 value", param2 = "param 2 value", ...)

Parameters:

Parameter name

Required

Default value

Description

message

Yes

 

validation error message.

key

No

 

i18n key for validation error message.

messageParams

No

 

Additional parameters to customize the message.

fieldName

No

 

Specifies field name in case this validator type is plain-validator.

shortCircuit

No

false

Whether this validator is short circuit.

type

No

ValidatorType.FIELD

type of the validator: field-validator (FIELD) or plain-validator (SIMPLE).

min

No

 

The minimum value to check.

minExpression

No

 

OGNL expression used to calculate the minimum value.

max

No

 

The maximum value to check

maxExpression

No

 

OGNL expression used to calculate the maximum value.

parse

No

false

Whether to evaluate the minExpressionand maxExpression to find min/max.

 

Struts @IntRangeFieldValidator Annotation Examples:

  • Basic field-validator:
    @IntRangeFieldValidator(min = "18", max = "25",
    		message = "Your age must be between ${min} and ${max}")
    public void setMyAge(int myAge) {
    	this.myAge = myAge;
    }

     

  • Specifying i18n key for the message:
    @IntRangeFieldValidator(min = "18", max = "25",
    		message = "Default message",
    		key = "form.validation.age")
    public void setMyAge(int myAge) {
    	this.myAge = myAge;
    }

     

  • Plain-validator (annotating the action method):
    @IntRangeFieldValidator(
    		type = ValidatorType.SIMPLE,
    		fieldName = "myAge",
    		min = "18", max = "25",
    		message = "Your age must be between ${min} and ${max}")
    public String execute() {
    	return SUCCESS;
    } 

 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment