• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

DynaValidatorForm - Double fields are never null!

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a DynaValidatorForm with field salary with type Double. Sometimes, the user types nothing in that field (isn't interested by that field) but after the validation process on server I get 0.0 as salary's value. Why ? How can I prevent this ? Is there a hidden setting for validator plug-in ?

Thanks
Cata
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this is a Struts question, I'm moving to the Web Application Frameworks forum.
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it is the Java itself feature, the default value for double.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be the default value of double. Just like other types. For example, for objects, they are set to null initially if nothing to set. For numeric data types, they are either 0, or 0.0d. The primitive data types cannot be null because they are NOT objects!

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have a DynaValidatorForm with field salary with type Double


By the way, what do you mean by type Double? Is it the wrapper object or primitive data type?

If it is the latter case, the default value should be null, NOT 0.0d.

Nick
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Catalinescu Mihai:
I have a DynaValidatorForm with field salary with type Double. Sometimes, the user types nothing in that field (isn't interested by that field) but after the validation process on server I get 0.0 as salary's value. Why ? How can I prevent this ? Is there a hidden setting for validator plug-in ?



Actually the setting of 0.0 for Double attributes has nothing to do with the validator framework. It is the struts framework which sets 0.0 for the attribute when it tries to populate the form bean from the request parameters.

I remember there being an init-param of the ActionServlet through which you can turn off this behaviour, i don't remember the name. It would then leave it as "null".

But there is a problem using "Double"s as attributes of form beans. If the user enters a non numeric value, the framework tries to convert it to Double, it fails, and it sets 0.0. Now, if you want to show the user his error, you end up with 0.0 and not the actual value entered.

The best way to get roung this is to keep all form attributes as "String" and once validation is completed you can convert it to the desired type for your use. The conversion in this case would not fail as validation is already completed.

Sheldon Fernandes
 
reply
    Bookmark Topic Watch Topic
  • New Topic