2

I was working on validation using annotation in Struts2 and i was quite surprised to see that the annotations does not have a LongRangeFieldValidator where as the validations done using xml does have a LongRangeFieldValidator

I tried different ways to get the LongRangeFieldValidor using annotations.

  1. LongRangeFieldValidator. It showed an error because it doesn't actually exists and com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator cannot be converted to an Annotation type. This was quite obvious so i switched to next.

  2. I used IntRangeFieldValidator. I could quite use it because it was unable to do a typecasting. I thought this should have worked because docs says it is for numeric types.

  3. DoubleRangeFieldValidator This one also validates (and it should) non-integer values so i had to drop this.

  4. Finally I had to convert my long field to a String and had to use RegexFieldValidator.

My question is why there isn't a LongRangeFieldValidator in the package com.opensymphony.xwork2.validator.annotations and what are the best practices to obtain it?

Roman C
  • 49,761
  • 33
  • 66
  • 176
maxx777
  • 1,320
  • 1
  • 20
  • 37
  • The short answer to "why" is "just because", likely an oversight. It's probably worth filing a JIRA ticket, though, since there should be one-to-one mappings between the XML and annotations, and we'll add it. – Dave Newton Apr 22 '14 at 14:08
  • As Dave said, feel free to register JIRA issue for that – Lukasz Lenart Apr 25 '14 at 12:40

1 Answers1

1

It seems they forgot to add this annotation to the core package. Just a mistake may be or so, but there is the workaround. Use a custom validator annotation

@CustomValidator(type ="long", fieldName = "myField") 

under registered validators you can find the name of the validator long.

Roman C
  • 49,761
  • 33
  • 66
  • 176