veote
veote

Reputation: 1480

How to reset input fields in a form after validation fail when updating it with new values

I'm using JSF 2 with PrimeFaces 4.0.

I have a form with some inputs and validation. Some fields may automatically filled when choosing an entity from a dialog. This works fine when I choose the entity before I submit the whole form (and validate). But when I choose an entity after validation the update of the component does not work.

JSF page:

<h:form id="form">
    <p:inputText id="id" value="#{bean.user.id}" required="true" 
        onclick="PF('usersDialog').show()" />
    <p:inputText id="name" value="#{bean.user.name}" required="true"
        onclick="PF('usersDialog').show()" />
    <p:commandButton value="submit" action="#{bean.submit}" update=":form" />
</h:form>

<p:dialog widgetVar="usersDialog">
    <h:form>
        <p:dataTable value="#{bean.users}" var="user">
            <p:column>
                <p:commandButton value="choose" onclick="PF('usersDialog').hide()"
                    process="@this" action="#{bean.select(user)}" update=":form" />
            </p:column>
            <p:column>
                <h:outputText value="#{user.name}" />
            </p:column>
        </p:dataTable>
    </h:form>
</p:dialog>

I know its up to the JSF lifecycle, but I'm not able to fix it. So how can I update the form after validation fail when choosing an new entity from dialog?

Thanks in advance.

Upvotes: 6

Views: 9451

Answers (2)

BalusC
BalusC

Reputation: 1109865

Exactly for this purpose, <p:resetInput> component was introduced (based on OmniFaces ResetInputAjaxActionListener).

Nest it inside the command button, with a target set to the same client ID as update attribute of command button:

<p:commandButton ... update=":form">
    <p:resetInput target=":form" />
</p:commandButton>

See also:

Upvotes: 12

Michele Mariotti
Michele Mariotti

Reputation: 7469

try this way:

<h:form id="form">
    <p:panel style="width: 80%;">
        <p:fieldset legend="Some User" id="costAuthUser">
            <p:panelGrid columns="3">
                <h:outputLabel for="costUserId">User-ID</h:outputLabel>
                <p:inputText id="costUserId" onclick="PF('costAuthDialog').show()"
                    value="#{createCert.costAuthUser.userId }" required="true"
                    requiredMessage="Bitte geben Sie die UserID ein." />
                <p:message for="costUserId" />

                <h:outputLabel for="costLastName">Name</h:outputLabel>
                <p:inputText id="costLastName"
                    onclick="PF('costAuthDialog').show()"
                    value="#{createCert.costAuthUser.lastName }" />
                <!-- <p:watermark value="" for="costLastName"/> -->
                <p:message for="costLastName" />
            </p:panelGrid>
        </p:fieldset>

        <p:separator />

        <p:commandButton value="submit" action="#{createCert.prepareEmail }" update=":form"/>

    </p:panel>
<h:form>


<p:dialog widgetVar="costAuthDialog" resizable="false" header="Some User">
    <h:form id="dialogForm">
        <p:dataTable value="#{createCert.costAuthList}" var="user">
            <p:column>
             <p:commandButton value="choose" process="@this"
                oncomplete="PF('costAuthDialog').hide()" action="#{createCert.selectCostAuth(user) }" update=":form"/>
            </p:column>

            <p:column headerText="Name">
                <h:outputText value="#{user.lastName}" />
            </p:column>
            <!-- Some more -->
        </p:dataTable>
    </h:form>
</p:dialog>

i think there's some issue re-rendering dialog hierarchies, this way dialog does not get updated.

and you should use oncomplete="PF('costAuthDialog').hide()" instead of onclick

Upvotes: 0

Related Questions