1
votes

This questions seems to be so simple, yet I didn't find the mistake.

Ok, here we go: I have a form with a List of IPs (whatever),
IP n: [INPUT-FIELD] [Button "Add"]
IP n+1: [INPUT-FIELD] [Button "Add"]
These IPs exist right from the start. They are initialized in the Constructor of the Managed Bean.

When I click on the "Add" Button, the current IP is validated. After that I add another entry to my java.util.List and another line in the JSF form appears:

IP n+2: [INPUT-Field] [Button "Add"]

and so on.

Adding a line (with a new IP) and validating the current IP work perfect for the existing values IP n and IP n+1, but validating and adding does NOT work with the dynamically added IP n+2. Neither validating the dynamically generated input field works nor adding another line with the newly created "Add"-Button.

What am I doing wrong?

<h:form id="frmSpecial" prependId="false">
    <h:panelGroup id="tblDestFw">
        <c:forEach items="#{bean.fwdest}" var="fwdest">
            IP: <h:inputText value="#{fwdest.ip}" validator="#{bean.validateIP}"/>
            <h:commandButton action="#{bean.addFwDest()}" 
                             value="&gt;&gt;" title="Add another line">
                <f:ajax execute="@this" render="tblDestFw"/>
            </h:commandButton>
            <br/>
        </c:forEach>
    </h:panelGroup>
</h:form>

@ManagedBean(name = "bean")
@ViewScoped
public class EnterNewSystemFW implements Serializable {
     List<FirewallDest> fwdest;
     public EnterNewSystemFW() {
         fwdest.add(new FirewallDest("N"));
         fwdest.add(new FirewallDest("N+1"));
     }

     public void addFwDest() {
         fwdest.add(new FirewallDest());
     }
}

Thanks.

Bernd

1

1 Answers

0
votes

As to adding the new line, your <c:forEach> references a view scoped bean property. Due to JSF issue 1492, this bean is reconstructed on every request.

Replace it by a normal JSF component like <ui:repeat> or <h:dataTable> and it'll work as expected.

<ui:repeat value="#{bean.fwdest}" var="fwdest">
    ...
</ui:repeat>

As to performing validation, your <f:ajax execute="@this"> processes only the current component (the command button), not the inputs. Use <f:ajax execute="@form"> instead to process the entire form.

See also: