Home > Visualforce > How to disable Visualforce inputFields

How to disable Visualforce inputFields

November 25th, 2009

Visualforce as a whole is great and is super easy to use once you get the hang of it but every once in a while I come across an issue that makes me go, “What the huh?”. A perfect example of this is that fact that there is no attribute to disable apex:inputFields in Visualforce, what the huh?!? Exactly. You can disable apex:inputText but not apex:inputFields. This can be frustrating as inputFields give you enhanced functionality such as the date picker for date fields, help hovers, and cleaner error messages.

So how can you disable an apex:inputField tag? I don’t know. Just kidding ;-P. Nearly all apex:inputFields when rendered for the browser become HTML input tags and input tags have a disabled attribute. What we can do is manipulate this value with a combination of Apex and Javascript.

Check out the working demo here.

For all my demos I am now trying to build packages so you can install them in your own orgs to play around with. The package is available here.

First is a very simple class. It has one Opportunity that we will bind our inputFields to, a Boolean to determine if the inputField should be disabled, and two methods to set the boolean to true and false.

public class disableInputField {
 
    public Opportunity opp {get; set;}
    public Boolean disableInput {get; set;}
 
    public disableInputField(){
        opp = new Opportunity();
    }
 
    public void disableCloseDateInput(){
        disableInput = true;
    }
 
    public void enableCloseDateInput(){
        disableInput = false;
    }
}

Next up is the page. What we do is place some inline Javascript right next to the input field we want to disable. When clicking the Enable/Disable buttons this will set the boolean value and rerender the oppName panel. When this happens the disableInput boolean value is merged in to the Javascript and since this is a inline Javascript statement it will execute every time the rerender is performed.

<apex:page controller="disableInputField">
    <apex:form >
        <apex:commandButton value="Disable inputField" action="{!disableCloseDateInput}" rerender="oppName"/>
        <apex:commandButton value="Enable inputField" action="{!enableCloseDateInput}" rerender="oppName"/><br/><br/>
 
        Opportunity Name:
        <apex:outputPanel id="oppName">
            <apex:inputField value="{!opp.CloseDate}" id="oppNameInput" required="false"/>
            <script>document.getElementById('{!$Component.oppNameInput}').disabled = {!disableInput}; </script>
        </apex:outputPanel>
    </apex:form>
</apex:page>

And that is how you can disable an inputField. Nice!

Categories: Visualforce
  1. December 14th, 2009 at 16:29 | #1

    I noticed you (too) avoid JavaScript where possible. I very quickly prototyped the ability to have the styleClass attribute pull from a Controller property and it worked. An alternative to this approach could be to add a styleClass property to your inputField and then have your enableCloseDateInput() and disableCloseDateInput() set that property. Tie that property value to valid CSS classes. Since you are re-rendering oppName it would work to disable your inputField. (I think)

  2. December 14th, 2009 at 16:40 | #2

    Very nice. I’ll give that a try and post back.

  3. December 14th, 2009 at 16:47 | #3

    Wait… I’m not aware of a css attribute for disabling a field. You can declare a style when it’s disabled but I don’t know how to disable the field with CSS.

  4. December 15th, 2009 at 05:15 | #4

    how to enable and disable a check box in a grid if while clicking edit and cancel edit or save.
    thanks

  5. December 15th, 2009 at 08:24 | #5

    d’oh! You are right. Wishful thinking I guess. I was hoping for a way to do it with CSS. Would be nice.

  6. XactiumBen
    December 17th, 2009 at 03:10 | #6

    Am I right in thinking this would require more javascript to disable the lookup field icon? I wish I would have seen this earlier – had to do some inputField disabling a few days ago. I went down the route of having two elements – inputField (when enabled) and inputText (when disabled). This is when I realised the apex messages tag would keep complaining if I tried to bind a number field to inputText. That was a very frustrating day…

  7. December 17th, 2009 at 08:37 | #7

    Yup, inputFields that are lookups are a bit trickier. I’m not exactly sure what to do with these but based on a quick glance it would definitely require some more advanced javascript. If you still have a need to disable these types of fields let me know and I may have time to look at it.

  8. yvk431
    December 17th, 2009 at 23:15 | #8

    @Jason
    Can we disable the Multi select Piclist using this :
    Please respond.