0

I have a form where there is a set of input textboxes and we are populating data as well as giving a provision to edit these. After editing we are showing the same page again with the edited details which is saved to database.

The problem is in cancel button click when we are modifying the data and instead of saving we are clicking on cancel button then the page is getting populated with edited data although we have clicked on cancel & its not saved in database the same page is getting rendered with the edited data. I want to avoid this scenario.

<h:outputText value="#{institutionalUser.userName}" rendered="#{not ProfileBean.editProfile}"></h:outputText>   
<p:inputText id="UserNameData"                                           value="#{organizationProfileBean.institutionalUser.userName}"                                              rendered="#{organizationProfileBean.editProfile}">

<h:commandButton styleClass="button" value="Cancel" id="cancelButton" immediate="true">

So in the above code is a part of companyorganisationprofile.xhtml page where we have the username having value from bean on page load. When we are going to edit the value we are using the textbox with the prepopulated value. When we are saving the data the same page is getting rendered again. But when we are clicking on cancel button the page is getting populated with the data which we have tried to edit but not inteded to save. The value is not getting saved still its showing in the page which is wrong.

I am not getting how to avoid to set the value in bean when we are not saving or clicking on cancel button.

So I want help in not to set value in bean when we are clicking on cancel button as it is not useful. Please share your valuable suggestion in this.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sandy
  • 93
  • 2
  • 9
  • 1: The fact that you label a button 'cancel' does not make it technically another type of button. So it behaves as a submit button. Learn about ajax and https://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes. 2: Same happens with a plain jsf inputtext, so not PrimeFaces related. – Kukeltje Jul 28 '17 at 14:18
  • I agree with you,but its nothing related to ajax. I want to avoid setting bean value in case of cancel button. – sandy Jul 28 '17 at 14:57
  • Thanks for your suggestion. – sandy Jul 28 '17 at 15:00
  • Start by asking why you use a commandButton... what is the purpose of your commandButton? If you **need** a commandButton and do mot show that in your code above, you **need** ajax – Kukeltje Jul 28 '17 at 15:01
  • I need a command button to either save or cancel the form details. While saving everything works fine but in case of cancel, suppose by mistake i entered something in text box which is not correct and i clicked on cancel button,its rendering the same page again with wrong entered bean value set as well as displayed. – sandy Jul 28 '17 at 15:05
  • Is this cancel button on the original page or on the second page which displays the edited values? Does the above code contain all the attributes for the command button, so it's just submitting the form? – kingdc Jul 28 '17 at 23:07
  • Hi Kingdc,this cancel button is in same page, actually we have same page for both viewing and editing data. Yes the above code contains all the attribute for the command button. There is no action attribute for the command button too. – sandy Jul 29 '17 at 01:57
  • Sandy, the default behavior of a commandButton is to submit the entire form it is enclosed in. Kukeltje was just asking why exactly you want to use a commandButton while you clearly don't want to submit the entire form. – BalusC Jul 29 '17 at 12:33

0 Answers0