XPages rendered fields issue

I mentioned last week that we're working on a large XPages Managed Bean project at the moment. I managed to come across a problem which seemed so basic that I couldn't believe that I'd not seen it before. Well after wasting a day on it, Stackoverflow came to the rescue with various people helping out. Just another demonstration of what a great community we are lucky enough to work with.

So my issue was related to progressive disclosure of fields. The idea was that field 1, a combo box was selected and then based on the value chosen, difference fields would be displayed. What was happening was that the fields were displayed, but their values weren't being stored when the document was saved.

The fields were being hidden and showed by computing their rendered property. But if I computed their styleClass property and hid or showed the field using CSS then it all worked fine, but that was obviously a hack.

So the underlying problem is associated with the JSF Page Lifecycle. If the field is not "rendered" into the base view of the page when it is first loaded then it will be ignored when submitting data back to the server. The solution is simple, we just have to added a check to our rendered computation to decide which stage of the lifecycle we're in. Unless we're in the final rendering phase then we will always "render" the field to the view, but when we get to the actual rendering phase we can apply our business logic and hide the field from the user's display.

In SSJS the code will look like this:

if( view.isRenderingPhase() ){
return MyBean.shouldIShowField();
}else{
return true;
}

In Java, then I'd recommend using Tip Tripcony's JSFUtil class, and then you can use something like...

public boolean showMyField(){
if (JSFUtil.getViewRoot().isRenderingPhase()){
if (shouldIShowMyField()){
return true;
}else{
return false;
}
}else{
return true;
}
}

I'm still unsure how I've never come across this issue before, but thanks to the XPages community at least I now have a solution.