[SalesForce] apex:commandbutton not refreshing outputpanel

My VF Page :

<apex:page controller="MyFirstCustomExpenseController">
   <apex:form >
    <apex:pagemessages />
   <apex:outputpanel id="op"> //Line 1
     <apex:pageblock title="sdfsdfsdf" id="pgb"> //Line 2
      <apex:pageblocksection columns="2">
       <apex:outputfield value="{!Expense.Id}" />
       <apex:inputField value="{!Expense.Expense_Date__c}"/>
       <apex:inputField value="{!Expense.Amount__c}"/>
      <apex:inputField value="{!Expense.Type__c}"/>
     <apex:inputField value="{!Expense.Sub_Type__c}"/>
       <apex:inputfield value="{!Expense.Comments__c}" id="fkc"/>
    </apex:pageblocksection>
  <apex:pageblockButtons >
   <apex:commandButton action="{!save}" value="Update" id="stb" rerender="op"/> //Line 3
   <apex:commandbutton action="{!test_pass}" value="Test Pass" />
  </apex:pageblockButtons>
   </apex:pageblock>
</apex:outputpanel>
</apex:form>
</apex:page>

Controller class

  public class MyFirstCustomExpenseController {

        public PageReference test_pass()  {
           ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Record Id - '+exp.Id));   
            return null;
        }


    private final Expense__c exp;

    public MyFirstCustomExpenseController() {
      exp = [SELECT ID,Name,Amount__c,Type__c,Sub_Type__c,Expense_Date__c,Comments__c FROM Expense__c WHERE ID = :ApexPages.CurrentPage().GetParameters().Get('id')];
    }

    public Expense__c getExpense() {
        return exp;
    }

    public PageReference save() {

        if(exp.Comments__c != ''){
          ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info,'Comments should be null'));}else{
           update exp;
       }
      return null;
    }
}

Objective :

In the VF page when an user tries to update an expense with non-null value in Comments an error message should be thrown and the outputpanel containing the data elements should be redrawn.

As soon as I put this rerender attribute in command button

<apex:commandButton action="{!save}" value="Update" id="stb" rerender="op"/> //Line 3

nothing happens 🙁

Neither the error message is shown to the user nor the output panel is getting refreshed.

Can someone tell me what I am missing ?

Best Answer

Couple of things I observed and may help to resolve the bug

1)No setter defined for your Expense variable.This may give you false expression that reRender is not happening

public void setExpense(Expense__c exp) {
    this.exp= exp;
}

2)the pagemessage tag needs to inside outputPanel to reflect error message when Rerender happens

  <apex:outputpanel id="op">
      <apex:pagemessages /><!--use pagemessage inside outputPanel thats reRendered-->
Related Topic