The double range field validator checks if a float/double field falls within a specified range. We can use this validator in either XML or annotation:

    • XML: specifying type=”double” attribute for <validator> or <field-validator> elements.
    • Annotation: specifying @DoubleRangeFieldValidator annotation type before field’s setter method or action method (plain-validator).
The range can be inclusive or exclusive. For example, if we specify a range of [10, 50] for a number x, then:

    • an inclusive range means: 10 <= x <= 50
    • an exclusive range means: 10 < x < 50
 

1. Struts Double Range Field Validator XML Syntax:

Usage:

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

  • Plain-validator syntax:
    <validator type="double">
    		<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).

minInclusive

No

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

maxInclusive

No

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

minExclusive

No

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

maxExclusive

No

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

minInclusiveExpression

No

OGNL expression to calculate the minimum inclusive value. It won’t be evaluated if omitted.

maxInclusiveExpression

No

OGNL expression to calculate the maximum inclusive value. It won’t be evaluated if omitted.

minExclusiveExpression

No

OGNL expression to calculate the minimum exclusive value. It won’t be evaluated if omitted.

maxExclusiveExpression

No

OGNL expression to calculate the maximum exclusive value. It won’t be evaluated if omitted.

 

Struts Double Range Field Validator XML Examples:

  • Field-validator example using inclusive range check:
    <field name="scale">
    	<field-validator type="double">
    		<param name="minInclusive">0.5</param>
    		<param name="maxInclusive">10.5</param>
    		<message>The scale must bet between ${minInclusive} and ${maxInclusive} (inclusive)</message>
    	</field-validator>
    </field>
  • Field-validator example using exclusive range check:
    <field name="scale">
    	<field-validator type="double">
    		<param name="minExclusive">0.5</param>
    		<param name="maxExclusive">10.5</param>
    		<message>The scale must be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
    	</field-validator>
    </field>
  • Only checking the minimum value:
    <field name="scale">
    	<field-validator type="double">
    		<param name="minInclusive">5.2</param>
    		<message>The scale must be equal or greater than ${minInclusive}</message>
    	</field-validator>
    </field>
  • Using OGNL expressions:
    <field name="scale">
    	<field-validator type="double">
    		<param name="minExclusiveExpression">${minExclusiveValue}</param>
    		<param name="maxExclusiveExpression">${maxExclusiveValue}</param>
    		<message>The scale must be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
    	</field-validator>
    </field> 
    In this case, the action class or the JavaBean class must implement the following methods (for example):

    public double getMinExclusiveValue() {
    	return 11.5;
    }
    
    public double getMaxExclusiveValue() {
    	return 40.3;
    } 
  • Plain-validator example:
    <validator type="double">
    	<param name="fieldName">scale</param>
    	<param name="minInclusive">0.5</param>
    	<param name="maxInclusive">10.5</param>
    	<message>The scale must bet between ${minInclusive} and ${maxInclusive} (inclusive)</message>
    </validator> 
 

2. Struts @DoubleRangeFieldValidator Annotation

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

@DoubleRangeFieldValidator(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).

minInclusive

No

 

The minimum inclusive value to check.

minInclusiveExpression

No

 

OGNL expression used to calculate the minimum inclusive value.

maxInclusive

No

 

The maximum inclusive value to check

maxInclusiveExpression

No

 

OGNL expression used to calculate the maximum inclusive value.

minExclusive

No

 

The minimum exclusive value to check.

minExclusiveExpression

No

 

OGNL expression used to calculate the minimum exclusive value.

maxExclusive

No

 

The maximum exclusive value to check.

maxExclusiveExpression

No

 

OGNL expression used to calculate the maximum exclusive value.



 

Struts @DoubleRangeFieldValidator Examples:

  • Basic field-validator:
    @DoubleRangeFieldValidator(
    		minInclusive = "23.5", maxInclusive = "57.8",
    		message = "The scale must bet between ${minInclusive} and ${maxInclusive} (inclusive)"
    		)
    public void setScale(double scale) {
    	this.scale = scale;
    }
     

  • Specifying i18n key for the message:
    @DoubleRangeFieldValidator(
    		minInclusive = "23.5", maxInclusive = "57.8",
    		message = "Default message",
    		key = "form.validation.scale"
    		)
    public void setScale(double scale) {
    	this.scale = scale;
    }
     

  • Plain-validator (annotating the action method):
    @DoubleRangeFieldValidator(
    		type = ValidatorType.SIMPLE,
    		fieldName = "scale",
    		minExclusive = "20.5",
    		maxExclusive = "78.5",
    		message = "The scale must be between ${minExclusive} and ${maxExclusive} (exclusive)"
    		)
    public String execute() {
    	return SUCCESS;
    } 
 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment