views:

31

answers:

2

Is it possible to use the java.util.Calendar in a Struts2 IntRangeFieldValidator?

The following code does not produce any OGNL errors but does not work either. I'm trying to create a validation rule for a year range, min= 1970 and max= current calendar year.

@RequiredFieldValidator(message="Year cannot be blank")
@IntRangeFieldValidator(message="Year must be greater than 1970", min="1970", max="java.util.Calendar.getInstance().get(java.util.Calendar.YEAR)", fieldName="year")
    public Integer getYear() {
        return year;
    }

Is there a better way to do this without writing a custom validator? Any/All replies are appreciated. Thanks in advance!

A: 

I think it should be:

@java.util.Calendar@getInstance().get(@java.util.Calendar@YEAR)

To be confirmed.

Samuel_xL
Nope. Didn't work. Thanks for the suggestion though.
Griff
@Griff Too bad. I know it works in JSPs though; maybe the "@" syntax cannot be used in annotations, since they're already using this character ? Good luck anyway.
Samuel_xL
A: 

Are you able to use the DateRangeFieldValidator?

http://struts.apache.org/2.0.11.2/struts2-core/apidocs/com/opensymphony/xwork2/validator/annotations/DateRangeFieldValidator.html

Here's a list of additional validators:

http://struts.apache.org/2.0.11.2/struts2-core/apidocs/com/opensymphony/xwork2/validator/annotations/package-summary.html

Ryan P.
It's not really a date field, it is just a number field that represents a year.
Griff
What if you hard code the max parameter to "2010" then try inputting 2011? Does it work then? If so, maybe it wants the int that's returned from calendar explicitly converted to a String value?String.valueOf(java.util.Calendar.getInstance().get(java.util.Calendar.YEAR))
Ryan P.
Interesting thought. String.valueOf(java.util.Calendar.getInstance().get(java.util.Calendar.YEAR)) did not work either.
Griff