Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jsf 2 - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...