function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sreeram ChakrapaniSreeram Chakrapani 

InputText Field Disabling on a VF Page entry

Hi,

I am looking to disable couple of fields when I launch my Visual Force Page from a Custom Button, can I get som help in terms of a sample code ?  I know this is basic but am new to Apex and Visual Force. Appreciate the help

Thanks
SC
Best Answer chosen by Sreeram Chakrapani
sunny522sunny522
Hi sreeram ,
   go through the example below.

apex class:

public class disableInputField1 {

    public Opportunity opp {get; set;}
    public Boolean disableInput {get; set;}

    public disableInputField1 (){
        opp = new Opportunity();
    }

    public void disableCloseDateInput(){
        disableInput = true;
    }

    public void enableCloseDateInput(){
        disableInput = false;
    }
}


visualforce page:

<apex:page controller="disableInputField1">
    <apex:form >
        <apex:commandButton value="Disable inputField" action="{!disableCloseDateInput}" rerender="oppName"/>
        <apex:commandButton value="Enable inputField" action="{!enableCloseDateInput}" rerender="oppName"/><br/><br/>

        Opportunity Name:&#37;
        <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>

Note :we can also disable input fields by using styles.
for example document.getElementById('elementid').style.display='none';

if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.


All Answers

Srik PothSrik Poth
Disable in sense, Do you want to null the fields or you do not want to show them on the VF Page?
Sreeram ChakrapaniSreeram Chakrapani
Hi Srikanth,

I want to show them in the Visual Force page but just like a ready only text field which is greyed out. Thanks in advance

SC
sbbsbb
I would use the <apex:outputField> tag. Like below:

<apex:outputField value="{!yourObject__c.yourField__c}"/>
sunny522sunny522
Hi sreeram ,
   go through the example below.

apex class:

public class disableInputField1 {

    public Opportunity opp {get; set;}
    public Boolean disableInput {get; set;}

    public disableInputField1 (){
        opp = new Opportunity();
    }

    public void disableCloseDateInput(){
        disableInput = true;
    }

    public void enableCloseDateInput(){
        disableInput = false;
    }
}


visualforce page:

<apex:page controller="disableInputField1">
    <apex:form >
        <apex:commandButton value="Disable inputField" action="{!disableCloseDateInput}" rerender="oppName"/>
        <apex:commandButton value="Enable inputField" action="{!enableCloseDateInput}" rerender="oppName"/><br/><br/>

        Opportunity Name:&#37;
        <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>

Note :we can also disable input fields by using styles.
for example document.getElementById('elementid').style.display='none';

if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.


This was selected as the best answer
tsalb_tsalb_
Generally for something this lightweight you should keep it client-side. I woul go with a Javascript function (you can use a command button or a vanilla HTML button) but just call a javascript function that sets your targetted elements with a property of disabled.

http://www.w3schools.com/jsref/prop_html_disabled.asp