• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF converstion and validation - null becomes zero

 
Bartek Bratek
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an input box that takes a number - a simple int. This box corresponds to a db column that accepts null values. I'd like to send null to DAO layer when user enters nothing (empty string).

The problem is that JSF automatically converts empty string to zero (int). I tried to provide my own converter, and I can convert the empty string into any value (say, -1 for example) but still not null! When my converter returns null DAO receives again zero.

I tried one more thing - marking the field as required and providing my own validator (that would ignore empty string):

<h:inputText id="canBeNull" value="#{controller.someField}" required="true">
<f:validator validatorId="myIntValidator"/>
<f:converter converterId="myIntConverter"/>
</h:inputText>

But here I ran into yet another problem: for some reason my validator is ignored and I always get a message "field required blah blah ...".

Does anyone know how to send that null to the db? I'm not sure what to try next ...


Thanks,

Bratek
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bratek Bratek welcome to Javaranch,
Firstly your name does not follow the Javaranch Naming policy.
please change it here.
Also take some time to read the ask good questions link below.

Thanks .
 
Andres Quinones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bartek if you want to allow the field to have empty Strings you should remove the required attribute or set it false.
[ July 16, 2008: Message edited by: Andres Quinones ]
 
Bartek Bratek
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only added the required="true" attribute to the input box to force JSF to call my own validator (which I thought would let me pass null; and JSF does not call that validator either way for some reason).

The original problem is that JSF is changing empty string into zero Integer. How to make it change empty string into null Integer?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bartek Bratek please check your private messages.
 
David Greenberg
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you answer this in a private message? I am facing the exact same problem. I use java.lang.Integer objects everywhere, but when leaving the text field blank, it resolves to 0.
 
David Greenberg
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like this may be related specifically to Tomcat 6.0.16:
http://www.experts-exchange.com/Web_Development/WebApplications/Q_23693081.html
 
J Wwww
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem. The reason was an issue related to tomcat 6.0.16. Since version 6.0.17 you can set a system property org.apache.el.parser.COERCE_TO_ZERO to false (default is true) to avoid converting empty strings to zero.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi J Wwww welcome to Javaranch ,
Well it looks like you display name does not the Javaranch naming policy . Please change it using the My Profile link above.
[ October 15, 2008: Message edited by: Amit Ghorpade ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!