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
posted
0
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
posted
0
Not sure what you folks are looking for but if you want to check for the messages of particular field then you can use
<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
posted
0
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..
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
posted
0
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:
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?