File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes validator-rule.xml how to guarantee the text-field is a double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validator-rule.xml how to guarantee the text-field is a double?" Watch "validator-rule.xml how to guarantee the text-field is a double?" New topic
Author

validator-rule.xml how to guarantee the text-field is a double?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi guys,

I don't want to rush into foolish conclusion but it looks like the validator-rule.xml is more of a trouble than a facilitator.

since I've already used it, can anyone tell me how to guarantee that the text field the user provide is a double?

I did this:

JSP:


and the validator double is:


SO...just as the maxlength works ok, what is the guarantee for a double? I tries something like:
double="true" or other 'creative' combination....nothing.

thanks
Manuel Jordan
Ranch Hand

Joined: Sep 29, 2006
Posts: 131
Hello Peter

can anyone tell me how to guarantee that the text field the user provide is a double?


how i did this??, mmm, (double a number with/out decimals) well, with regex, With regex you can control the amount of numbers after the dot ".###", if the number should negative or not, maybe start with a range (higher than 100)

i think that with regex is more fexible, furthermore remember this, all the variables of your actionForm are String (a simple class of course without composition).


kill your pride, share your knowledge with all
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm not sure what you're asking. Since you say you've used it, I assume you know how to apply the double validation rule to this field. If your question is "How does the Struts code validate a double?", the best way to find out would be to download the source code and look at the code in the org.apache.struts.validator.FieldChecks class.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
You seem a little confused about validation, so I will throw out the basics of enabling validation:

1) Your Action form should extend ValidatorForm (or ValidatorActionForm or one of the dynamic forms).

2) Set validate="true" on your action mapping (it is the default).

3) Add an entry to your validation.xml file like this:

<form name="ProductForm">
<field property="purchaseCost" depends="double">
<arg0 key="Field.Product.PurchaseCost" />
</field>
</form>

4) Define ValidatorPlugIn in your struts-config.xml file.

- Brent
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I'm using the validation.xml but when I put a string value (instead of a number) I get the following error:
javax.servlet.ServletException: For input string: "h"
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I was under the impression the the validation-rules will take care of incorrect type.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I am not exactly sure what would cause that exception. My guess is that since the stack trace shows that the code got to processActionPerform, then your execute method is being called and it is your code that is throwing the exception. If you posted your ActionForm and Action code then maybe somebody can help out further.

- Brent
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
The error comes from the action (the db gets a String (not double) and throws an error)

So...the problem is that it *GETS* to the action without the validity of the dynaActionForm. If the dynaActionForm would have thrown an error BEFORE it gets to the action that would've solved the problem (unfortunately, it doesn't).

I have the validation-rule.xml

but that doesn't kicks in.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Carefully review the steps that Brent outlined in his first post. I suspect that you will find one or more of them incomplete or missing. If you still can't find the error, please post:

* The entry for this field in your validation.xml file
* your ActionForm
* Relevant portions of the JSP
* relevant message keys in your ApplicationResources.properties file
* relevant portions of your struts-config.xml file
[ November 14, 2006: Message edited by: Merrill Higginson ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Here is a link with more details: http://www.oracle.com/technology/oramag/oracle/04-jan/o14dev_struts.html

Have you gotten other validation checks to work?

- Brent
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
ok, this is my resolution: I realized that the form needs more than just checking for double (also date conflicts etc); so it would be much easier to do that with a regular form (not dynaActionForm). Anyhow, this solved the problem.

I guess that when it comes to long (problematic) forms one should not use the dynactionform (but that's just my observation)

thanks for your help at least I got familiar with xml validation.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validator-rule.xml how to guarantee the text-field is a double?