0

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.

Patan
  • 17,073
  • 36
  • 124
  • 198

1 Answers1

4

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.

<h:commandButton value="reset" action="#{bean.reset}">
    <f:ajax render="@form" resetValues="true"/>
</h:commandButton>
public void reset() {
    value1 = null;
    value2 = null;
}

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.

<h:commandButton value="reset" type="reset" />

Much better is just refreshing the page.

<h:button value="reset" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks. I tried to print the value in model and I get it as null value. But when I try to print value at component I get value. So the values are not updated to model yet because validation fails. do you think it as strange. – Patan Sep 04 '15 at 14:48
  • Hm? Did you apply the answer on your code? Doesn't sound like so. It's not strange if you understand the problem. It's in detail elaborated in the first "See also" link. – BalusC Sep 04 '15 at 14:49
  • When I set values to null, the older values from database are set to null. so I am afraid I f I loose my old values. – Patan Sep 04 '15 at 14:51
  • Value of v1 #{v1comp.value}--I get entered value #{bean.value1}--I get null value – Patan Sep 04 '15 at 14:53
  • Uh, the `reset()` method in the answer is exemplary and based on solely the information provided in the question. You should there just reinitialize the model to the desired values. You have nowhere stated in the question that they are initially non-null. – BalusC Sep 04 '15 at 14:54