• cdi
  • components
  • contexts
  • converters
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • renderkits
  • resourcehandlers
  • scripts
  • taghandlers
  • utils
  • validators
-
  • DefaultPhaseListener
  • InvokeActionEventListener
  • ResetInputAjaxActionListener

Description

The org.omnifaces.eventlistener.ResetInputAjaxActionListener will reset input fields which are not executed during ajax submit, but which are rendered/updated during ajax response. This will prevent those input fields to remain in an invalidated state because of a validation failure during a previous request. This is very useful for cases where you need to update one form from another form by for example a modal dialog, or when you need a cancel/clear button. For a more detailed explanation, check this Stack Overflow answer)

Register it as <phase-listener> in faces-config.xml. It'll be applied to every single ajax action throughout the webapp, on both UIInput and UICommand components.


<lifecycle>
    <phase-listener>org.omnifaces.eventlistener.ResetInputAjaxActionListener</phase-listener>
</lifecycle>
		

Or register it as <action-listener> in faces-config.xml. It'll only be applied to ajax actions which are invoked by an UICommand component such as <h:commandButton> and <h:commandLink>.


<application>
    <action-listener>org.omnifaces.eventlistener.ResetInputAjaxActionListener</action-listener>
</application>
		

Or register it as <f:actionListener> on the invidivual UICommand components where this action listener is absolutely necessary to solve the concrete problem. Note that it isn't possible to register it on the individual UIInput components using the standard JSF tags.


<h:commandButton value="Update" action="#{bean.updateOtherInputs}">
    <f:ajax execute="currentInputs" render="otherInputs" />
    <f:actionListener type="org.omnifaces.eventlistener.ResetInputAjaxActionListener" />
</h:commandButton>
		

Note that PrimeFaces has implemented the same idea later in its new version 3.4 <p:resetInput>, so if you happen to use PrimeFaces already and want to apply it on specific command buttons only, then you may want to use it instead.

Demo

Form without ResetInputAjaxActionListener

input1: Enter something (but don't enter "Updated!").

input2: Leave this field empty or enter a non-numeric value to cause a validation failure.

Press "submit" and then "update". The "update" simulates changing model values externally.

Problem: input1 is not updated with text "Updated!". And when MyFaces is used, input2 isn't updated with text "42" either.


Same form, with ResetInputAjaxActionListener on "update"

input1: Enter something (but don't enter "Updated!").

input2: Leave this field empty or enter a non-numeric value to cause a validation failure.

Press "submit" and then "update". The "update" simulates changing model values externally.

Solved: input1 is now updated with text "Updated!".

Source code
<h3>Form without <code>ResetInputAjaxActionListener</code></h3>
<h:form id="form1">
    <p>input1: Enter something (but don't enter "Updated!").</p>
    <p>
        <h:inputText id="input1" value="#{resetInputBean.input1}" required="true" />
        <h:message for="input1" />
    </p>
    <p>input2: Leave this field empty or enter a non-numeric value to cause a validation failure.</p>
    <p>
        <h:inputText id="input2" value="#{resetInputBean.input2}" required="true" />
        <h:message for="input2" />
    </p>
    <p>Press "submit" and then "update". The "update" simulates changing model values externally.</p>
    <p>
        <h:commandButton value="Submit">
            <f:ajax execute="@form" render="@form" />
        </h:commandButton>
        <h:commandButton value="Update" action="#{resetInputBean.update}" >
            <f:ajax execute="@this" render="@form" />
        </h:commandButton>
        <h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
    </p>
    <p><b>Problem</b>: input1 is not updated with text "Updated!". And when MyFaces is used, input2 isn't updated with text "42" either.</p>
</h:form>

<hr />

<h3>Same form, with <code>ResetInputAjaxActionListener</code> on "update"</h3>
<h:form id="form2">
    <p>input1: Enter something (but don't enter "Updated!").</p>
    <p>
        <h:inputText id="input1" value="#{resetInputBean.input1}" required="true" />
        <h:message for="input1" />
    </p>
    <p>input2: Leave this field empty or enter a non-numeric value to cause a validation failure.</p>
    <p>
        <h:inputText id="input2" value="#{resetInputBean.input2}" required="true" />
        <h:message for="input2" />
    </p>
    <p>Press "submit" and then "update". The "update" simulates changing model values externally.</p>
    <p>
        <h:commandButton value="Submit">
            <f:ajax execute="@form" render="@form" />
        </h:commandButton>
        <h:commandButton value="Update" action="#{resetInputBean.update}" >
            <f:ajax execute="@this" render="@form" />
            <f:actionListener type="org.omnifaces.eventlistener.ResetInputAjaxActionListener" />
        </h:commandButton>
        <h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
    </p>
    <p><b>Solved</b>: input1 is now updated with text "Updated!".</p>
</h:form>