Reputation: 669
I have a field that has a required attribute. When I press the Accept button to save some data without entering any value in the field, an error message is displayed. So far so good.
But, if right after that I decide to click on the Cancel button, that error message overrides the confirmation message that is supposed to be displayed inside a <p:dialog/>
element.
NOTE: If instead I use the <p:confirmDialog/>
component, there seems to be no problem because, I guess, it uses a message=""
attribute, no the <p:messages/>
tag.
XHTML
<p:dialog>
<p:outputPanel>
<h:form>
<h:outputText value="Field:"/>
<p:inputText id="field" value="" type="text" required="true" requiredMessage="You must complete the field" />
<p:growl id="messages" showDetail="true"/>
<p:commandButton id="dialogCancel" value="Cancel" oncomplete="confirmCancelDialog.show();" actionListener="#{controller.addCloseWarn}" />
</h:form>
</p:outputPanel>
</p:dialog>
<h:form>
<p:dialog id="confirmCancelDialog" header="Warning!" widgetVar="confirmCancelDialog" modal="true" >
<p:messages id="closeMessage" showDetail="true" autoUpdate="true" />
<p:commandButton id="confirm" value="Accept" onclick="..." />
<p:commandButton id="decline" value="Cancel" onclick="..." />
</p:dialog>
</h:form>
Bean controller
public void addCloseWarn(ActionEvent actionEvent) {
FacesContext.getCurrentInstance().addMessage("closeMessage", new FacesMessage(FacesMessage.SEVERITY_WARN, null,
"Are you sure you want to leave the page?"));
}
Upvotes: 1
Views: 11537
Reputation: 14277
Problem with Cancel button is that your form is submitted and validation is executed. You can add process="@this"
attribute to commandButton
, so other parts of form will not be processed and your addCloseWarn
method will be executed.
I would also add that this is probably not standard use of message
tag. It is used to show errors, warning and successful messages, not confirmation questions. So use confirmDialog
or use standard dialog with just ordinary text and OK - Cancel buttons.
Upvotes: 2