title: How can I reset JSF UIInput components to their managed bean values
body: <p>I want to reset JSF inputs to their original managed bean values after validation failed.</p>
  
  <p>I have two forms inside the same page - the first form has a <code>commandLink</code> to initialize the second form.  The second form is rendered as a dialog whose visibility is toggled through jQuery - for the purpose of this exercise, though, I can illustrate just with two forms on the same page.  Also, while I'm using PrimeFaces 2.2.x in my app, the same behaviors appear with regular <code>h:commandLink</code> as well. </p>
  
  <p>The issue I'm having is:</p>
  
  <ol>
  <li>click link in first form to initialize second form</li>
  <li>submit invalid values in second form</li>
  <li>click link in first form again to initialize second form - <em>invalid values still there</em> and/or UIInput state is still invalid.</li>
  </ol>
  
  <p>For example - take the following form</p>
  
  <pre><code>&lt;h:form id="pageForm"&gt;
      &lt;h:commandLink actionListener="#{testBean.initialize}"&gt;Initialize, no execute
          &lt;f:ajax render=":dialogForm"/&gt;
      &lt;/h:commandLink&gt;
      &lt;br/&gt;
      &lt;h:commandLink actionListener="#{testBean.initialize}"&gt;Initialize, execute=@this
          &lt;f:ajax execute="@this" render=":dialogForm"/&gt;
      &lt;/h:commandLink&gt;
  
  &lt;/h:form&gt;
  
  &lt;h:form id="dialogForm"&gt;
      &lt;h:messages/&gt;
      String property - Valid: &lt;h:outputText value="#{property.valid}"/&gt;
      &lt;br/&gt;
      &lt;h:inputText id="property" binding="#{property}" value="#{testBean.property}"&gt;
      &lt;f:validateLength minimum="3"/&gt;                         
      &lt;/h:inputText&gt;
      &lt;br /&gt;
      Int property - Valid: &lt;h:outputText value="#{intValue.valid}"/&gt;
      &lt;h:inputText id="intValue" binding="#{intValue}" value="#{testBean.intValue}"&gt;
          &lt;f:validateLongRange maximum="50" /&gt;                            
      &lt;/h:inputText&gt;
      &lt;br/&gt;
  
      &lt;h:commandLink actionListener="#{testBean.submit}"&gt;
          Submit
          &lt;f:ajax render="@form" execute="@form"/&gt;
      &lt;/h:commandLink&gt;
  
      &lt;h:commandLink actionListener="#{testBean.initialize}"&gt;Initialize, execute=@this
          &lt;f:ajax execute="@this" render="@form"/&gt;
      &lt;/h:commandLink&gt;
  
  &lt;/h:form&gt;
  </code></pre>
  
  <p>Bean class:</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class TestBean {
    private String property = "init";
    private Integer intValue = 33;
    // plus getters/setters
  
    public void submit() { ... }
    public void initialize() {
     intValue = 33;
     property = "init";   
    }
  
  }
  </code></pre>
  
  <p><strong>Behavior #1</strong></p>
  
  <ol>
  <li>click either "Initialize" link on the pageForm</li>
  <li>inputs get initialized to "init", "33"</li>
  <li>now submit something invalid for both fields like "aa", "99"</li>
  <li>now click any of the "initialize" links again (they all seem to behave the same - makes no difference whether it's in the same form or different, or whether I have specified execute="@this" or not.) </li>
  </ol>
  
  <p>Result => <code>UIInput.isValid()</code> = false, both values reset though ("init", "33").</p>
  
  <p>Expected => valid = true (or is this not reasonable to expect?) </p>
  
  <p><strong>Behavior #2</strong></p>
  
  <ol>
  <li>click either "Initialize" link on the pageForm</li>
  <li>inputs get initialized to "init", "33"</li>
  <li>now submit something invalid for the text field but valid for the int field ("aa", "44")</li>
  <li>now click any of the "initialize" links again</li>
  </ol>
  
  <p>Result => "init", valid=false; 44, valid=true</p>
  
  <p>Expected => "init", valid=true; 33, valid=true</p>
  
  <p>I have also looked at:</p>
  
  <p><a href="https://stackoverflow.com/questions/3933786/jsf-2-bean-validation-validation-failed-empty-values-are-replaced-with-las">JSF 2 - Bean Validation: validation failed -&gt; empty values are replaced with last valid values from managed bean</a></p>
  
  <p>and</p>
  
  <p><a href="https://stackoverflow.com/questions/6642242/how-can-i-populate-a-text-field-using-primefaces-ajax-after-validation-errors-oc">How can I populate a text field using PrimeFaces AJAX after validation errors occur?</a></p>
  
  <p>The suggestion to explicitly reset the state of <code>UIInputs</code> with <code>resetValue</code> <em>does</em> work, but I'm not happy with it.</p>
  
  <p>Now, I sort of understand why the isValid is not resetting - my understanding of the JSF lifecycle is that once a value is submitted to a component, isValid is not reset until the component is successfully submitted and validated and the Update Model Values phase sets the bean value.  So there may be no way around explicitly resetting the valid state in this case, since I want to use <code>#{foo.valid}</code> for conditional CSS styling.  </p>
  
  <p>What I don't understand, though, is why the components that successfully validated are not re-initializing from the bean.  Perhaps my understanding of the JSF lifecycle is slightly off?  </p>
  
  <p>I understand the rules layed out in the answer to <a href="https://stackoverflow.com/questions/6642242/how-can-i-populate-a-text-field-using-primefaces-ajax-after-validation-errors-oc">How can I populate a text field using PrimeFaces AJAX after validation errors occur?</a> as they pertain to an individual component but not to the form as a whole - i.e., what happens if a component succeeds validation but the validation overall fails?  </p>