• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to make empty text fields not pass null as the value, need them to just not pass anything

 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a device page where a user inputs a device name and optionally, latitude and longitude for the device. Here is the relevant code from the jsf page:

The values of these latitude and longitude fields are taken and posted to some xml on a web service through this java code (the relevant part):

If I put in a device, let's say named ABBA, and put in values for the lat and lng fields, like 100 and 122, it results in this xml:

Just like it should. However, if I leave the latitude and longitude fields blank it results in this xml:

When I need it to result in this xml:

So I'm not sure if this means I need to make a change in the java code, which seems like it will be more difficult, or a change in the jsf code, which I am hoping because it seems like it would be a simple thing like just adding some attribute.

Thinking about it, it may be simple to do this through the java code, doing something with an if statement and only do device.setProperties(properties); if lat and lng are not null.
I think I probably just solved this so I am about to try it out, but if there is a way to do it through the jsf please let me know. Should I maybe remove immediate=true from those two input fields?
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF doesn't understand "not pass anything". If you display a form and then post it back, the entire form is considered valid input. There are some subtleties about this process based on how the underlying HTML works, but JSF is going hide that, so it doesn't make any difference.

If a parameter is optional, you'll have to handle that in the backend code. You can either add explicit switches on the form or you can test for a field left blank, like so:

if ( longitude.trim().length() > 0 ) {
properties.put("lng", deviceLng);
}

Note that this means that longitude has to be defined as a text property, so you should also supply an appropriate validator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!