0

EDIT:

<apex:commandButton value="Save" rerender="IdPanel" 
disabled="{!AND(SelectedText == null && SelectedText == '')}" />
    <apex:outputPanel id="IdPanel">
      <apex:repeat var="TheID" value="{!CurrentList}"> 
    <apex:outputText value="{!TheID}" />;
      </apex:repeat>
  </apex:outputPanel> 

How can I enable/disable commandButton based on the user have selected?

Here I'm checking to see if the User have selectedText and if the SelectedText is null or empty I want the commandButton to be disabled but the below code does disable the button but but never enable even though I have value in the SelectedText

<apex:commandButton value="Save" rerender="IdPanel" 
disabled="{!AND(SelectedText == null && SelectedText == '')}" />
7
  • Are you rerendering the container (e.g. Apex:OutputPanel) that the commandButton is in?
    – Dan Jones
    Commented Aug 10, 2015 at 14:45
  • I have updated my question please have a look
    – Nick
    Commented Aug 10, 2015 at 14:47
  • 3
    How can SelectedText be both null and an empty string at the same time? Would an OR not be better?
    – BarCotter
    Commented Aug 10, 2015 at 14:50
  • yes make no diff if I replace with || (OR)
    – Nick
    Commented Aug 10, 2015 at 14:51
  • Can you add more detail to what you want to happen? Do you want the button to be disabled as soon as they fill in the SelectedText text? or is this something that happens after they click Save?
    – BarCotter
    Commented Aug 10, 2015 at 15:00

2 Answers 2

2

Without knowing much about your code, the following works assuming you have something like...

public String selectedText {
  get;
  set;
}

... In your controller.

See below:

<apex:form>
    <apex:actionFunction name="myActFunct" reRender="myPanel" />
    <apex:inputText value="{!selectedText}" onkeyup="myActFunct();" />
    <apex:outputPanel id="myPanel">
        <apex:commandButton value="Save" rerender="IdPanel" disabled="{!AND(SelectedText == null || SelectedText == '')}" />
    </apex:outputPanel>
</apex:form>

Basically you've got your inputText which, onkeyup, calls the ActionFunction which then rerenders the panel and then can check whether or not SelectedText is empty or not.

I've tested this and it should meet your requirements.

Good luck!

2
  • 1
    oops.. didnt see your answer before posting mine. Slight change in mine that instead of actionFunction, I used actionSupport. :-)
    – AslamK
    Commented Aug 10, 2015 at 15:24
  • No problem! Both ways work which is the important thing. :)
    – Dan Jones
    Commented Aug 10, 2015 at 15:34
0

Its quite simple to achieve this. You just need to capture the keyup action and rerender the section containing the CommandButton. Use ActionSupport to capture the events as shown below.

Also added an actionStatus so that you know that the re-rendering is happening.

  <apex:pageBlock id="someblock">
        <apex:inputText value="{!SelectedText}">
            <apex:actionSupport event="onkeyup" reRender="commandButtonPanel" status="status"/>
        </apex:inputText>

        <apex:outputPanel id="commandButtonPanel">
            <apex:commandButton action="{!enable}" disabled="{!SelectedText==null}" value="Submit"/>
        </apex:outputPanel>

        <apex:actionStatus startText="Re-evalutaing..."  stopText="Idle" id="status"/>
    </apex:pageBlock>

You must log in to answer this question.

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