2

I have the following VF page:

<apex:page controller="Invoices">
<apex:form >      
<apex:pageBlock id="pageBlock1">

<apex:pageMessages id="messages" />

    <apex:outputLabel value="Select month" />

    <apex:selectList value="{!invoiceMonth}" id="invoiceMonth" label="Month" rendered="true"> 
        <apex:actionSupport event="onchange" reRender="buttonPanel" />
        <apex:selectOption itemValue="0" itemLabel="This Month" />
        <apex:selectOption itemValue="1" itemLabel="Last Month" />
        <apex:selectOption itemValue="2" itemLabel="2 Months Ago" />
    </apex:selectList>

<apex:outputPanel id="buttonPanel">

    <apex:commandButton action="{!createInvoices}" value="Create Invoices" disabled="{!If(invoiceMonth = null, true, false)}" reRender="pageBlock1"> 
    </apex:commandButton>

</apex:outputPanel>    

</apex:pageBlock>

</apex:form>
</apex:page>

Messages are set in the controller using ApexPages.addmessage()

The page messages were working fine, then I added to the command button the attribute:

disabled="{!If(invoiceMonth = null, true, false)}"

This disables the button until a value is chosen from the selectList, but when this attribute is present the pageMessages no longer work when the button is clicked.

I have tried moving the pageMessages component around, inside the outputPanel, in a different page block. I have also tried rerendering the messages when the commandButton is clicked.

Any ideas?

3
  • Note that you can always replace IF(condition, true, false) with simply condition.
    – Adrian Larson
    Jan 24, 2017 at 0:31
  • 1
    Have you tried rerendering the pageMessages tag from the actionSupport? The only thing it currently rerenders is the buttonPanel. <apex:actionSupport event="onchange" reRender="buttonPanel, messages" />
    – Mark Pond
    Jan 24, 2017 at 0:51
  • I have tried that. Still doesn't work unfortunately.
    – josh_mam
    Jan 24, 2017 at 2:42

1 Answer 1

0

I did not find any issue with the code provided.
At lease I am sure from the Visualforce page. You may be doing something wrong in controller. Tryout the following working code (it is yours code):-

enter image description here

Page:

<apex:page controller="PageMessageDemoController">
<apex:form>      
<apex:pageBlock id="pageBlock1">

<apex:pageMessages id="messages" />

    <apex:outputLabel value="Select month" />

    <apex:selectList value="{!invoiceMonth}" id="invoiceMonth" label="Month" rendered="true" size="1"> 
        <apex:actionSupport event="onchange" reRender="buttonPanel" />
        <apex:selectOption itemValue="0" itemLabel="This Month" />
        <apex:selectOption itemValue="1" itemLabel="Last Month" />
        <apex:selectOption itemValue="2" itemLabel="2 Months Ago" />
    </apex:selectList>

<apex:outputPanel id="buttonPanel">

    <apex:commandButton action="{!createInvoices}" value="Create Invoices" disabled="{!If(invoiceMonth = null, true, false)}" reRender="pageBlock1"> 
    </apex:commandButton>

</apex:outputPanel>    

</apex:pageBlock>

</apex:form>

Controller:-

public class PageMessageDemoController 
{           
        public String invoiceMonth {get;set;}
        public PageMessageDemoController()
        {
            invoiceMonth = '';
        }

        public void createInvoices()
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,'Hello, invoice is '+invoiceMonth));
        }

}
2
  • As I said, all the messages were working fine right until I added disabled="{!If(invoiceMonth = null, true, false)}" so I am sure the controller is fine. If the Visualforce page is fine too then maybe it is a bug.
    – josh_mam
    Jan 24, 2017 at 3:51
  • I have also added "disabled="{!If(invoiceMonth = null, true, false)}" in button. Is it not working when you remove this attribute? @josh_mam
    – Ashwani
    Jan 24, 2017 at 4:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .