Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to combine JSP <c:if> with JSF <f:validateLength>  RSS feed

 
Trevor Whitney
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

So here is the goal. I'm creating a form for our intranet at work. The form populates a staff directory, but can also be used to create a new email and domain account for new hires. If the latter is being done, the last 4 digits of the new employee's SSN need to be included. If only their contact information is being added, the SSN field can be left blank. So, a value is entered into the SSN field, I would like to validate it, but if it's left blank the value will be 0, and there is no need to run validation on it. So I tried this:



This, of course, didn't work. Since the form does not actually complete, due to a few required fields I left blank, the "last4SSN" property is not being set. Thus, when I try to test if it's greater than 0, well, I have no value to evaluate. That leads to my question: How can I get the value that is currently in the <h:inputText> box using JSP or JSF? I can think of a solution using JavaScript, but I would like to do this entirely with JSP and JSF tags.

So, anyone know how I can set up a conditional <f:validateLength> tag, that only executes if the current value of the <h:inputText> box is greater than 0?

Thanks for the help!

-Trevor
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a rule, you should forget JSTL when coding for JSF. They're 2 different architectures with 2 different approaches to problem-solving.

You might check to see if you really need this much work. I had it recently pointed out to me that some validators only validate if data is actually entered. This may actually require a blank field, however, rather than "0". But blank makes more sense for this sort of thing anyway.
 
Trevor Whitney
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Tim.

But blank makes more sense for this sort of thing anyway


How do you do this while simultaneously associating the value with an int property in my bean? Since the value of this is set to "#{NewUser.last4SSN}", and that is an integer variable, it defaults to 0, thus displaying a 0 in the input field instead of a blank. I think a blank might solve my problem, but I can't figure out how to do that. The validator does run when I don't touch the field and leave it at 0.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a matter for research. Although an SSN isn't really an integer, so don't let the fact that it's numeric force you into false limitations. A String property with a 4-digit regular expression validator might be a better choice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!