• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 4864
  • 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'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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

reply
    Bookmark Topic Watch Topic
  • New Topic