wood burning stoves 2.0*
The moose likes JSF and the fly likes JSF check if message is empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF check if message is empty" Watch "JSF check if message is empty" New topic
Author

JSF check if message is empty

vivek ja
Ranch Hand

Joined: Feb 24, 2005
Posts: 80
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

Joined: Jul 22, 2004
Posts: 66
I am also interested in figuring out how to do this. Anyone have an idea?
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
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


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
J Haley
Ranch Hand

Joined: Jul 22, 2004
Posts: 66
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

Joined: May 22, 2008
Posts: 14
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

Joined: Jan 23, 2010
Posts: 1
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

Joined: Jun 29, 2009
Posts: 3
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

Joined: Jun 25, 2001
Posts: 15956
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Benny Bottema
Greenhorn

Joined: Aug 16, 2011
Posts: 1
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

Joined: Jun 30, 2011
Posts: 46

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?


Independent Programmer - Technology Enthusiast - JUG Leader from JUG Joglosemar
Alexandre Simundi
Greenhorn

Joined: Sep 05, 2011
Posts: 1
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
 
Consider Paul's rocket mass heater.
 
subject: JSF check if message is empty
 
Similar Threads
customizing messages
request.getParameter == null
SHOW h:message giving problem
Validation error messages location
empty input validation