I’m trying to collect values of an UIInput component inside an UIData component during bean’s action method in order to validate duplicate values. I tried to bind the UIInput component to a bean property and getting its value, but it prints null. If I place it outside the datatable, then it prints the expected value. Is there something wrong with the datatable?
<rich:dataTable binding="#{bean.table}" value="#{bean.data}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Field1" />
</f:facet>
<h:inputText binding="#{bean.input}" value="#{item.field1}" />
</h:column>
</rich:dataTable>
Here’s the backing bean code:
private UIData table;
private UIInput input;
public void save() {
System.out.println(input.getId() + " - " + input.getValue());
}
There’s nothing wrong with the datatable. There’s only one
UIInputcomponent in the JSF component tree whose state get changed everytime whenever the parentUIDatacomponent iterates over every item of the model. The state is thus only available during theUIDataiteration and not before or after. You’re trying to access the value of a single component in the bean’s action method while the parentUIDatacomponent isn’t iterating over it, so the values will always returnnull.You need to visit the component tree by
UIComponent#visitTree()on theUIDataand collect the information of interest in theVisitCallbackimplementation.By the way, you’d normally perform the validation with a normal
Validatoron theUIInputcomponent or, in this particular case maybe better, aValueChangeListener. This allows for easier invalidation and message handling.