my form looks like this:

When the user clicks “+ Add” a new distance input shows up:

The user can repeat this operation as many times as desired, but all distances must be filled in, and individual messages must be served for missing data:

So i’m trying to stablish dynamic ids for the distance inputs. However, when i try to pass dynamic values everything fails. up to now i’ve tried two different techniques:
- using the binding HtmlDataTable’s rowIndex <- it always shows -1
- setting the id as an attribute of the Distance class <- ugly exception
here’s my code:
Backing bean:
private List<Distance> distances;
private HtmlDataTable distancesUI;
public void addDistance(ActionEvent e) {
distances.add(new Distance());
}
JSPX:
<h:dataTable
binding="#{Bean.distancesUI}"
value="#{Bean.distances}"
var="distance">
<h:column>
<h:inputText
id ="distance_#{Bean.distancesUI.rowIndex}" // <- always renders "distance_-1"
value="#{distance.value}" />
</h:column>
</h:dataTable>
I need to be able to show individual “required” messages. Any ideas?
thanks in advance
For that you don’t need to fiddle with IDs. JSF will do it for you. Just do
This will end up in generated HTML like follows:
Rightclick the page in browser and choose View Source to see it yourself.
Any validation error will just be shown in the same row as the input.