• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts2 Validation - Number?

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using the following code to validate number range:



It works fine for number ranges, such as entering 4 or 10238, but it doesn't seem to validate that the value is a number. For example, if I enter "Jasda" for the value it doesn't throw a validation failed message. I know I can use regex to resolve this, but it seems there should be something in double validator or a related class that does this?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you running the default stack? Assuming the rest of the validation XML is correct and the property is actually a number there should probably be a type conversion error. These are added to the error messages by the "conversion" interceptor. More info might help diagnose the problem more easily.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the more I search on this issue the more people I come across that indicate double/int validators only validate ranges and ignore non-numeric values. In other words, I have to use regex to test if something is a number. Seems odd that there's a built-in numeric range validator without a simpler one for whether or not the value is a number. According to comments in other forums, this differs from Struts 1 behavior.

Here's one example article: http://digg.com/software/Struts_2_Integer_Validation_Example
Can't anything explicit in the documentation though.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

I have also face same problem.

I have implement integer validation. It validate fine but when I use input beyond the range of integer value my validation get failed.

So, still I am not getting solution for this.

For Ex. I use 12321312332 for int input in converts in to -ve value.


 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Scott: That hasn't been my experience, as I use integer validation frequently (and regex validation infrequently). If it *is* true, then it's a recently-introduced bug and should have an issue filed against it. As you're reluctant to provide any more information, I can't help beyond that.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you want for an example, I just used the standard double field validator with a min and max inclusion/exclusion parameters.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are you running the default stack? Assuming the rest of the validation XML is correct and the property is actually a number there should probably be a type conversion error. These are added to the error messages by the "conversion" interceptor. More info might help diagnose the problem more easily.

 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm running the default everything, just a simple validation test using a simple struts2 tutorial. I'm using HTML markup to show the validation errors and it works correctly for x > max or x < min, but does not handle x not being a number.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the JSP?
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the entire validation file and the S2 config file? What version of S2 are you using?

I can't reproduce this and have never seen this issue. I get a "Invalid field value for field 'age'" if I type in "Jasda".
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you configuring the message for "Invalid field value for field 'age'"?

Struts.xml:


HelloAction-validation.xml:
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not; it's the default message for type conversion errors.

What is the type of the "age" action property? You're attempting to validate both a string ("requiredstring") and a double ("double")--it can't be both.

If it's a Double (although I'd argue it should be an Integer; floating-point ages are a recipe for tears and angst) then to make it required it should be a "required" validator, not "requiredstring". Note that using a primitive double property may not always do what you want, since primitive doubles have a default value of 0.0.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this test its a number, but in my actual code its a string based on the work I'm doing (its within a list of strings). Despite it being a string both requiredstring and double validators work just fine, ie the appropriate message comes up for both, with the exception of the non-number strings.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I guess it would make sense if age was a number for it to throw an error converting age within Java. But this would more in line with a java exception then it would be an explicit validation error. So if I understand it correctly, Struts2 is missing the validation directly and relying on java exceptions to 'check' the bad data? Or in other words, there's no way to check a string is a number using a built in validator, but you can check its within a given range with build in functions.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I've stated, it's a type conversion error. These errors are handled when S2 tries to set a value to a property and the property cannot be converted to the property's type: in this case, "Jasda" can't be converted into a double, so there's a type conversion error.

You can make a string *look* like a number via a regex, but that doesn't *make* it a number. It's not a matter of "missing" the validation, it's that you're ignoring the built-in type conversion. Personally I don't *want* the "double" validation to work on strings, because strings don't have numeric values. (If asked I would have said it wouldn't work, but I guess it does.)

I guess the bottom line is that you either make your numbers numbers, or make them strings and treat them as such.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With that logic, though, the double/int range validators shouldn't work on strings, but they do. I just find it odd they would make that exception for strings on range and not make it on whether or not the value is a number.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, that's why I said I would have expected it not to work. I'm guessing they do their own conversion inside the validator and silently blow up on conversion failure, but I don't have the XWork source in front of me. Turning up the log levels on XWork might reveal more about the validation process.
reply
    Bookmark Topic Watch Topic
  • New Topic