Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF check if message is empty

 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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!
 
J Haley
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also interested in figuring out how to do this. Anyone have an idea?
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Rajshekhar Anand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
jaime collins
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering the same thing.

<hutputText 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?
 
kripa shankar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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..
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18214
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaime collins wrote:I am wondering the same thing.

<hutputText 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.

 
Benny Bottema
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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:

 
Yosi Pramajaya
Ranch Hand
Posts: 46
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • 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?

 
Alexandre Simundi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic