aspose file tools*
The moose likes Struts and the fly likes Struts2 Validation - Number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 Validation - Number?" Watch "Struts2 Validation - Number?" New topic
Author

Struts2 Validation - Number?

Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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?


My Blog: Down Home Country Coding with Scott Selikoff
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689


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.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

Can you post the JSP?
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Where are you configuring the message for "Invalid field value for field 'age'"?

Struts.xml:


HelloAction-validation.xml:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
 
 
subject: Struts2 Validation - Number?