The Problem
I have a form where the top quarter is Employee Information. The bottom 3 quarters are issue details about the employee. When the form is first loaded all the employee information is blank. There is a field for Employee ID and a Lookup button. When the ID is typed in and the Lookup button pressed, the Empoyee Information fields are populated accordingly. Everything worked great, until I added validation.
There are only 2 fields in the bottom part of my form that need to be validated. Issue Type and Summary. Both of these fields are required for every issue. The problem, if you haven't guessed already, is that when I would click on the Lookup button, the page would validate and I would recieve errors about the 2 required fields. Enter the immediate attribute.
I added immediate="true" to my Lookup button. Then I redeployed the application for testing. Clicking the Lookup button now submitted the page, however, none of my employee information was filled in. Looking at my log files I noticed that my ObjectNotFound exception was being thrown because the Employee ID field was blank. After some investigation it seems to be that since the immediate="true" cause the action to be executed during the "Apply Request Values" phase, the Employee ID is not even being put in the request. Now, I am not an expert on JSF's phases but saying that an attribute does something and then coming in behind and saying that it's only in a specific phase makes for strange implementation.
Interestingly enough, all the examples that demonstrate using the immediate attribute are with Cancel buttons. So to correct all the documentation out there on the immediate attribute...
Only use immedate="true" if you need to cancel the current form without causing validation.
The Solution
Well, there isn't a solution. This is just how it is. But I did a workaround. Instead of doing automatic validation on those 2 fields, when my Save button is clicked and my saveIssue method is called, the first thing I do is check those 2 fields for validity. If either fails I simply added a message to the FacesContext and then return my failure string so the issue page is displayed with the appropriate error messages.
If anyone knows a better solution for this problem I would love to hear it. I would also like to know if there is anything in the next JSF release that is going to change the behavior of the immediate attribute. It seems like it would have been better to just copy ASP.NET on this one and have an attribute called causesValidation. And then this would simply skip the validation phase.
When using immediate="true" on a commandButton as in your situation, I would use actionListener attribute and in it's method call processUpdate.
public void actionListenerMethod(ActionEvent e){
e.getComponent().processUpdates();
}
Read more...





(You should still be able to get them, but referencing UI controls directly -- not backing model bean).
Think of it as a _not_so_easy_ way of doing actions while still having values deffered from commiting to backing model. (and afaik asp.net doesn't have something like MVC even implemented, then you really shouldn't compare those two approches)
Cheers, a.