Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DynaValidatorForm - Double fields are never null!

 
Catalin Mihalache
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is a Struts question, I'm moving to the Web Application Frameworks forum.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it is the Java itself feature, the default value for double.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic