Problem:
When user clicks a save button, the entered values are validated in a Backing bean method based on a business rule. If the validation fails, a confirmation message or pop up should be displayed to the user whether the user wishes to continue with save.
Solution:
This is little tricky. We call the validation method inside the action method of save button. We have to use a hidden parameter whose value is set to true if the validation fails ie we need to prompt the user. Using a javascript check if the value of hidden parameter is true then show the confirmation message. When user clicks ok in the confirmation message, from the javascript call the action (save) method of a hidden command button. Call the javascript on loading the page
Here is the javascript
function showConfirmation(){
var value=document.getElementById('formId:valid').value;
if(value == "true"){
if(confirm("There are values that are at 0.Do you want to save the information ?")){
document.getElementById('formId:hiddenButton').click();
}else{
return false
}
}
}
jsf page:
<body onload=showConfirmation()>
<h:form id="formId">
<h:commandButton id="save" type="submit" value="Save" action="#{BackingBean.save}"/>
<h:inputHidden id="valid" value="#{BackingBean.showconfirmation}"/>
<h:commandButton id="hiddenButton" value="hidden" action="#{BackingBean.saveWithZero}" style="visibility:hidden;" />
</h:form>
</body>
Make sure that you set showconfirmation=false in your backing bean until you need to show the pop up.