title: Clear form in p:dialog on close after failed action/method call
body: <p>I've got a form within a <code>p:dialog</code> component being submitted via AJAX. The form in the dialog is via a <code>ui:include</code> of another <code>.xhtml</code> containing just the form and its components (no, I am not nesting forms; the dialog itself is not within any form). The included page's backing bean is 'ViewScoped'.</p>
  
  <p>MOST things function fine: </p>
  
  <ul>
  <li>On successful validation, the form will execute the save method; after a successful save, the dialog closes; opening a subsequent record shows proper record information from the database. </li>
  <li>On unsuccessful validation, the submitted values persist and the dialog remains open and displays validation errors, fixing the invalid fields and resubmitting results in a successful save.</li>
  </ul>
  
  <p>The problem comes in when the <code>action</code> called by the <code>p:commandButton</code> fails. In the event that it fails, the dialog remains open and displays "Error saving changes" via <code>h:messages</code> to the user; additionally, the submitted values still persist in the input fields. This is fine (and even desired) while the form remains open, however-- upon closing the dialog and reopening it, the submitted values are STILL in the text fields. This is bad because it gives the user the impression that the save did succeed (as the <code>h:messages</code> component is now blank due to the dialog update).</p>
  
  <p>So far I have tried the following, NONE of which have remedied the problem:</p>
  
  <ul>
  <li>1.1) Setting the backing bean of the included form to a new instance of the bean on dialog open.</li>
  <li>1.2) Since the bean is now a new instance, I am also repopulating the address object with details from a data bean.</li>
  <li>2.1) Disabling the close button on the dialog and adding a <code>p:commandButton</code> "Cancel" button with a <code>p:resetInput</code> component in
  it to close the dialog/reset the form. (dialog closes, but submitted
  values in form persist).</li>
  <li>2.2) Calling a method from this <code>p:commandButton</code> that removes the <code>RequestMap</code> for the included form's backing bean.</li>
  <li>2.3) Tried with the button set to both <code>immediate=true</code> and <code>immediate=false</code>.</li>
  <li>2.4) Tried with the button set to <code>process=@this</code>, a similar tactic I have used to close a form to ensure it will have fresh fields upon
  reopening.</li>
  </ul>
  
  <p>You would think between setting the backing bean to a new instance, and repopulating the address object I would be seeing a fresh form, but NOPE.</p>
  
  <p>Here is some of the source:</p>
  
  <p><strong>Portion of mainform.xhtml</strong></p>
  
  <pre><code>    &lt;p:dialog id="dlgAddress"
                header="Address Edit"
                widgetVar="dialogAddress"
                dynamic="true"
                modal="false"
                closable="false"
                resizable="false"
                styleClass="dlgAddress"
                visible="#{addressform.showDlgAddress}"&gt;
          &lt;ui:include src="addressform.xhtml"/&gt;
      &lt;/p:dialog&gt;
  </code></pre>
  
  <p><strong>addressform.xhtml</strong></p>
  
  <pre><code>&lt;h:form id="fDlgAddress"&gt;
      &lt;div id="addresses-container"&gt;
          &lt;h:messages id="msgDlgAddress" errorClass="errormsg" infoClass="infomsg1" layout="table"/&gt;
          &lt;h:panelGrid columns="1"
                       styleClass="pgAddresses"
                       rendered="#{!addressform.address.exists}"&gt;
              &lt;h:outputText value="No active #{addressform.address.atypCode} address found."
                            styleClass="record-not-exists"/&gt;
              &lt;h:outputText value="Use the form below to create one."/&gt;
          &lt;/h:panelGrid&gt;
          &lt;p:panelGrid columns="2"
                       styleClass="pgDlgForm pgAddresses"&gt;
              &lt;h:outputLabel value="Address Type"/&gt;
              &lt;h:inputText id="atypCode1"
                           value="#{addressform.address.atypCode}"
                           disabled="true"
                           rendered="#{addressform.address.exists}"/&gt;
              &lt;h:selectOneMenu id="atypCode2"
                               value="#{addressform.address.atypCode}"
                               rendered="#{!addressform.address.exists}"&gt;
                  &lt;f:selectItems value="#{addressform.atypCodeList}"
                                 var="atyp"
                                 itemValue="#{atyp}"
                                 itemLabel="#{atyp}"/&gt;
              &lt;/h:selectOneMenu&gt;
              &lt;h:outputLabel value="Street 1" 
                             for="street1"/&gt;
              &lt;h:inputText id="street1"
                           value="#{addressform.address.addressLine1}"/&gt;
              &lt;h:outputLabel value="Street 2" 
                             for="street2"/&gt;
              &lt;h:inputText id="street2"
                           value="#{addressform.address.addressLine2}"/&gt;
              &lt;h:outputLabel value="Street 3" 
                             for="street3"/&gt;
              &lt;h:inputText id="street3"
                           value="#{addressform.address.addressLine3}"/&gt;
              &lt;h:outputLabel value="Street 4" 
                             for="street4"/&gt;
              &lt;h:inputText id="street4"
                           value="#{addressform.address.addressLine4}"/&gt;
              &lt;h:outputLabel value="City" 
                             for="city"/&gt;
              &lt;h:inputText id="city"
                           value="#{addressform.address.city}"
                           required="true"
                           requiredMessage="Please enter a city."/&gt;
              &lt;h:outputLabel value="State" 
                             for="statCode"/&gt;
              &lt;h:selectOneMenu id="statCode"
                               value="#{addressform.address.stateCode}"&gt;
                  &lt;f:selectItem itemLabel="Select State/Province"
                                itemValue=""/&gt;
                  &lt;f:selectItems value="#{states.statesList}"
                                 var="stat"
                                 itemValue="#{stat.statCode}"
                                 itemLabel="#{stat.statDesc}"/&gt;
              &lt;/h:selectOneMenu&gt;
              &lt;h:outputLabel value="Zip Code" 
                             for="zipCode"/&gt;
              &lt;h:inputText id="zipCode"
                           value="#{addressform.address.zip}"/&gt;
              &lt;h:outputLabel value="Country" 
                             for="natnCode"/&gt;
              &lt;h:selectOneMenu id="natnCode"
                               value="#{addressform.address.nationCode}"
                               required="true"
                               requiredMessage="Please choose a nation."&gt;
                  &lt;f:selectItem itemLabel="Select Country"
                                itemValue=""/&gt;
                  &lt;f:selectItems value="#{nations.nationsList}"
                                 var="natn"
                                 itemValue="#{natn.natnCode}"
                                 itemLabel="#{natn.natnDesc}"/&gt;
              &lt;/h:selectOneMenu&gt;
              &lt;h:outputLabel value="From Date" 
                             for="fromDate"/&gt;
              &lt;p:calendar id="fromDate"
                          value="#{addressform.address.fromDate}"
                          showButtonPanel="true"/&gt;
              &lt;h:outputLabel value="To Date" 
                             for="toDate"/&gt;
              &lt;p:calendar id="toDate"
                          value="#{addressform.address.toDate}"
                          showButtonPanel="true"/&gt;
              &lt;h:outputLabel value="Inactivate"
                             for="inactivateAddress"
                             rendered="#{addressform.address.exists}"/&gt;
              &lt;h:selectBooleanCheckbox id="inactivateAddress"
                                       value="#{addressform.address.inactivate}"
                                       rendered="#{addressform.address.exists}"/&gt;
              &lt;h:outputLabel value="Delete"
                             for="deleteAddress"
                             rendered="#{addressform.address.exists}"/&gt;
              &lt;h:selectBooleanCheckbox id="deleteAddress"
                                       value="#{addressform.address.delete}"
                                       rendered="#{addressform.address.exists}"/&gt;
          &lt;/p:panelGrid&gt;
      &lt;/div&gt;
      &lt;div class="button-container"&gt;
          &lt;p:commandButton value="Save Changes"
                           action="#{addressform.save}"
                           type="submit"
                           ajax="true"
                           process="@form"
                           update="@form"/&gt;
          &lt;p:commandButton value="Cancel"
                           process="@this"
                           onclick="dialogAddress.hide();"&gt;
              &lt;p:resetInput target="fDlgAddress"/&gt;
          &lt;/p:commandButton&gt;
      &lt;/div&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p><strong>Recorddetailsform (backing bean for form from which address dialog is called)</strong></p>
  
  <pre><code>public void showDlgAddress(){
          FacesContext ctx = FacesContext.getCurrentInstance();
          ELResolver resolver = ctx.getApplication().getELResolver();
          RecordDetails recordDetails = (RecordDetails) resolver.getValue(ctx.getELContext(), null, "recordDetails");
  
          //Set addressform backing bean to new instance and load address into it from recordDetails
          resolver.setValue(ctx.getELContext(), null, "addressform", new Addressform());
          Addressform addressform = (Addressform) resolver.getValue(ctx.getELContext(), null, "addressform");
          addressform.setAddress(recordDetails.getAddress());
      }
  </code></pre>
  
  <p><strong>Addressform (address form's backing bean)</strong></p>
  
  <pre><code>public void save() {
      FacesContext ctx = FacesContext.getCurrentInstance();
      RequestContext rctx = RequestContext.getCurrentInstance();
      ELResolver resolver = ctx.getApplication().getELResolver();
      Records records = (Records) resolver.getValue(ctx.getELContext(), null, "records");
      RecordDetails recordDetails = (RecordDetails) resolver.getValue(ctx.getELContext(), null, "recordDetails");
      Mainform mainform = (Mainform) resolver.getValue(ctx.getELContext(), null, "mainform");
      Person person = (Person) resolver.getValue(ctx.getELContext(), null, "person");
  
      //Pretty lengthy SQL stuff here. Commented out. Just wanted to display the display logic below for the dialog.
  
      if (errorMsg != null) {//If errorMsg is not null, then error occurred.
          showDlgAddress = true;//Ensures address dialog remains open in event of action error.
          queueErrorMessage(errorMsg);
          rctx.update("dlgAddress");
          return;//break out of method on error.
      } else {
          showDlgAddress = false;
          rctx.update("dlgAddress");
      }
  
      //If everything saves without error, repopulate address and update recordDetails dialog.
      recordDetails.populateAddress(records.getSelectedRecord());
      mainform.updateDlgRecordDetails();
  }
  </code></pre>
  
  <p>Other Info:</p>
  
  <ul>
  <li>JSF2 </li>
  <li>Primefaces 3.5 </li>
  <li>Tomcat 6.0 </li>
  <li>Netbeans</li>
  </ul>