I've got a form within a p:dialog
component being submitted via AJAX. The form in the dialog is via a ui:include
of another .xhtml
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'.
MOST things function fine:
The problem comes in when the action
called by the p:commandButton
fails. In the event that it fails, the dialog remains open and displays "Error saving changes" via h:messages
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 h:messages
component is now blank due to the dialog update).
So far I have tried the following, NONE of which have remedied the problem:
p:commandButton
"Cancel" button with a p:resetInput
component in
it to close the dialog/reset the form. (dialog closes, but submitted
values in form persist).p:commandButton
that removes the RequestMap
for the included form's backing bean.immediate=true
and immediate=false
.process=@this
, a similar tactic I have used to close a form to ensure it will have fresh fields upon
reopening.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.
Here is some of the source:
Portion of mainform.xhtml
<p:dialog id="dlgAddress"
header="Address Edit"
widgetVar="dialogAddress"
dynamic="true"
modal="false"
closable="false"
resizable="false"
styleClass="dlgAddress"
visible="#{addressform.showDlgAddress}">
<ui:include src="addressform.xhtml"/>
</p:dialog>
addressform.xhtml
<h:form id="fDlgAddress">
<div id="addresses-container">
<h:messages id="msgDlgAddress" errorClass="errormsg" infoClass="infomsg1" layout="table"/>
<h:panelGrid columns="1"
styleClass="pgAddresses"
rendered="#{!addressform.address.exists}">
<h:outputText value="No active #{addressform.address.atypCode} address found."
styleClass="record-not-exists"/>
<h:outputText value="Use the form below to create one."/>
</h:panelGrid>
<p:panelGrid columns="2"
styleClass="pgDlgForm pgAddresses">
<h:outputLabel value="Address Type"/>
<h:inputText id="atypCode1"
value="#{addressform.address.atypCode}"
disabled="true"
rendered="#{addressform.address.exists}"/>
<h:selectOneMenu id="atypCode2"
value="#{addressform.address.atypCode}"
rendered="#{!addressform.address.exists}">
<f:selectItems value="#{addressform.atypCodeList}"
var="atyp"
itemValue="#{atyp}"
itemLabel="#{atyp}"/>
</h:selectOneMenu>
<h:outputLabel value="Street 1"
for="street1"/>
<h:inputText id="street1"
value="#{addressform.address.addressLine1}"/>
<h:outputLabel value="Street 2"
for="street2"/>
<h:inputText id="street2"
value="#{addressform.address.addressLine2}"/>
<h:outputLabel value="Street 3"
for="street3"/>
<h:inputText id="street3"
value="#{addressform.address.addressLine3}"/>
<h:outputLabel value="Street 4"
for="street4"/>
<h:inputText id="street4"
value="#{addressform.address.addressLine4}"/>
<h:outputLabel value="City"
for="city"/>
<h:inputText id="city"
value="#{addressform.address.city}"
required="true"
requiredMessage="Please enter a city."/>
<h:outputLabel value="State"
for="statCode"/>
<h:selectOneMenu id="statCode"
value="#{addressform.address.stateCode}">
<f:selectItem itemLabel="Select State/Province"
itemValue=""/>
<f:selectItems value="#{states.statesList}"
var="stat"
itemValue="#{stat.statCode}"
itemLabel="#{stat.statDesc}"/>
</h:selectOneMenu>
<h:outputLabel value="Zip Code"
for="zipCode"/>
<h:inputText id="zipCode"
value="#{addressform.address.zip}"/>
<h:outputLabel value="Country"
for="natnCode"/>
<h:selectOneMenu id="natnCode"
value="#{addressform.address.nationCode}"
required="true"
requiredMessage="Please choose a nation.">
<f:selectItem itemLabel="Select Country"
itemValue=""/>
<f:selectItems value="#{nations.nationsList}"
var="natn"
itemValue="#{natn.natnCode}"
itemLabel="#{natn.natnDesc}"/>
</h:selectOneMenu>
<h:outputLabel value="From Date"
for="fromDate"/>
<p:calendar id="fromDate"
value="#{addressform.address.fromDate}"
showButtonPanel="true"/>
<h:outputLabel value="To Date"
for="toDate"/>
<p:calendar id="toDate"
value="#{addressform.address.toDate}"
showButtonPanel="true"/>
<h:outputLabel value="Inactivate"
for="inactivateAddress"
rendered="#{addressform.address.exists}"/>
<h:selectBooleanCheckbox id="inactivateAddress"
value="#{addressform.address.inactivate}"
rendered="#{addressform.address.exists}"/>
<h:outputLabel value="Delete"
for="deleteAddress"
rendered="#{addressform.address.exists}"/>
<h:selectBooleanCheckbox id="deleteAddress"
value="#{addressform.address.delete}"
rendered="#{addressform.address.exists}"/>
</p:panelGrid>
</div>
<div class="button-container">
<p:commandButton value="Save Changes"
action="#{addressform.save}"
type="submit"
ajax="true"
process="@form"
update="@form"/>
<p:commandButton value="Cancel"
process="@this"
onclick="dialogAddress.hide();">
<p:resetInput target="fDlgAddress"/>
</p:commandButton>
</div>
</h:form>
Recorddetailsform (backing bean for form from which address dialog is called)
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());
}
Addressform (address form's backing bean)
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();
}
Other Info:
Try this:
Put your dialog inside a form (dialogInputForm)
Add the attribute update=":dialogInputForm"
The commandButton:
<p:commandButton process="@this" actionListener="#{bean.prepare}" update=":dialogInputForm" oncomplete="thirdPartyDialog.show()" value="Open Input Dialog" >
<p:resetInput target=":dialogInputForm" />
</p:commandButton>
And the form which contais the dialog
<h:form id="dialogInputForm" >
... your dialog ...
</h:form>
This worked for me, place this ajax event in the dialog. When the dialog is closed, the validations will be cleaned
<p:dialog id="dlgAddress"
header="Address Edit"
widgetVar="dialogAddress"
dynamic="true"
modal="false"
closable="false"
resizable="false"
styleClass="dlgAddress"
visible="#{addressform.showDlgAddress}">
<ui:include src="addressform.xhtml"/>
<p:ajax event="close" update="dlgAddress" resetValues="true" />
</p:dialog>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With