• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF check if message is empty

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to do some cross field validations in JSF. I am doing it manually in my backing bean.
Is there a way to check if the message for a particular field is empty?

<c:if test="#{ empty message.userName}" >
doesnt seem to be working.I know there is a way to check if there are any messages using rendered="#{! empty facesContext.maximumSeverity}"
But in my case I want to check for specific field error messages.
Depending on that I have to apply a new stylesheet to my text box. Can someone help. Thanks!
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also interested in figuring out how to do this. Anyone have an idea?
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you folks are looking for but if you want to check for the messages of particular field then you can use

<h:inputText id="username" value="#{LoginBean.username}" required="true">

and then

<h:message for="username" />

Hope this helps.

Thanks,
Rahul
 
J Haley
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an error occurs for a particular field, I want to show something using a rendered.

How to test for any error.


But how would I do this for a particular error?

[ November 19, 2008: Message edited by: J Haley ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't get your exact req.

but you can use , validateLength to make sure null values doesn't goes.

http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/f/validateLength.html


further , you can customize the validator message


-Gaurav
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am wondering the same thing.

<h:outputText value="passwords must be 6 characters long." rendered="#{facesContext.messages.contains(INVALID_PASS)}" />

In other words, I would like to render the above output if an only if facesContext.getMessages contained the message INVALID PASSWORD. Which was set when the password validator determined that the password entered was not in correct format.

This has nothing to do with page validation!! So, checking if fields are null dont apply to this problem.

Any ideas?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I can tel you a work around but not a solution. Why cant you use a boolean in backing bean that toggles based of the error message and bind that boolean to the outputtext component in the front end.. this will work only if the case is like you have only two mesages... If you want to change the visiblity of the text box based on many messages, you can se scriptlet by which you can extract the faces context object and thus the error message...

This is just a work around though, cos using scriptlets is highly discouraged, considering many factors as you might ve known.. So, if your project is in real cruch point use this.. else dont..
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jaime collins wrote:I am wondering the same thing.

<h:outputText value="passwords must be 6 characters long." rendered="#{facesContext.messages.contains(INVALID_PASS)}" />

In other words, I would like to render the above output if an only if facesContext.getMessages contained the message INVALID PASSWORD. Which was set when the password validator determined that the password entered was not in correct format.

This has nothing to do with page validation!! So, checking if fields are null dont apply to this problem.

Any ideas?



I'd go with kripa's approach instead of coding FacesContext references directly in EL. However, we're talking several different contexts here.

For Jaime's problem, the solution would most likely be to actually put a length validator on the password field. You can have more than one validator for an element! Let it generate the appropriate message.

Lacking details, I can't offer specifics on the more abstract cases mentioned, but it's possible to write a custom validator for cross-field validation checking. I have one that takes 2 password fields and determines whether they both have the same value.

In the JSF lifecyle, the backing bean doesn't get informed if any of the validations fail, since validation occurs earlier in the lifecycle than bean updating and actions and validation failures will short-circuit those phases. That puts a few constraints on where you can locate this kind of logic.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a solution that is somewhat cleaner:

Register in Facelets a global method that you will be able to call from any of your pages. This is the only clean way (in JSF 1.2 at least) to call a method and pass in arguments:

1). If you haven't already, register a taglib file with your web.xml. This will be used by Facelets


2). Register a function with your-tag-lib.xml


3). Create a org.foo.JsfUti equivalent class that will host your static function and add the function



4). Call the new function from your page




That's it. Two remarks though:

One, I've had some trouble getting the foo:hasMessage call to work when combined with other code within #{}. I needed to assign the value in a ui:param first so that I could use the result together with some other conditions.


Two, from a separation-of-concerns point of view (JSF being an MVC framework afterall), I think it's cleaner to check whether there are messages for a specific client id, instead of checking for specific message codes. Here's a version I used to do just that:

 
Ranch Hand
Posts: 48
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for solving the error message problem, maybe you should not use the default error message from JSF.
You can add it by yourself, right?

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

Benny Bottema wrote:



How do you pass the clientId to this method??

Btw, thanks a lot for your comment. It's saved my ass.

Thanks,
Alexandre Simundi
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic