I have a form with a reset button. The purpose of this button is to reset the values of the form. This works fine when there are no validation errors.
When there is validation error, it does not work.
<h:form id="form">
<h:inputText id="v1" value="#{bean.value1}"/>
<h:inputText id="v2" value="#{bean.value2}" required="true"/>
<h:commandLink value="submit" action="#{bean.submit}">
<f:ajax render="v1 v2"/>
</h:commandLink>
<h:commandButton value="reset" action="#{bean.dummy}">
<f:ajax render="@form" resetValues="true"/>
</h:commandButton>
</h:form>
When I try to reset the values before clicking submit button, all values are reset.
When I enter some value at field v1 and no value in field v2, I get validation message as expected when save is pressed.
Now, I try to resetValues by clicking the reset button. It just resets the invalid field v2. The valid field v1 is not reset.
Am I missing any thing.
The
resetValues
has a somewhat misleading name. It resets only the component state. It doesn't reset the model values. You're still responsible for that yourself.An alternative is putting
type="reset"
in the button, which will reset the form to its initial state as it was when the page was presented to the enduser.Much better is just refreshing the page.
See also: