Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 380
veote Avatar asked Jan 17 '14 06:01

veote


1 Answers

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:

  • How can I populate a text field using PrimeFaces AJAX after validation errors occur?
  • Reset input fields without executing validation
like image 162
BalusC Avatar answered Nov 01 '22 06:11

BalusC