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
sehoop27sehoop27 

Text field dependent on pick list field - VisualForce help needed

I have seen that a number of people have experienced my issue of wanting to have a text field dependent on a pick list field -- meaning that if a certain value was selected in a pick list, then the text field would require data entry.  (e.g. if user selects 'Other', then user must complete Comments field).
 
I do not want to manage this via field validation that occurs when the record is saved.  I would also prefer not to achieve this with an S-control.  I'm thinking that this is best achieved with VisualForce.
 
Ideally, I would like for the text field (i.e. Comments field) to display upon selection of the 'Other' pick list value.  (IT would remain hidden if the 'Other' pick list value was not selected.)
 
I believe that this is done with a partial page refresh.  I am looking for help in the form of a simple example of house this would be coded in a VisualForce page.
 
Thanks,
Scott
Arpit Gupta 40Arpit Gupta 40
I just write a simple vf page. I think this one will be your answer:
<apex:page standardController="Account" sidebar="false">
<apex:form >
<apex:pageBlock >
   <apex:pagemessages />
   <apex:pageBlockSection id="pm" columns="1" title="Dependent Text">
        <!-- Your picklist Field -->
        <apex:inputField value="{!Account.Picklist_Field__c}">
        <apex:actionSupport event="onchange" rerender="pm" status="status"/>
        </apex:inputField>
        <apex:panelGrid id="stdterm" rendered="{!Account.Picklist_Field__c=='Other'}" >
        <!-- Comment text field appear when picklist value= 'Other' -->
        <apex:outputText >Comments &nbsp;&nbsp;&nbsp;&nbsp;<apex:inputField value="{!Account.Comments__c}"/></apex:outputText>
        </apex:panelGrid>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

Please mark Best answer, if you will find it correct.
 
Glyn Anderson 3Glyn Anderson 3
Arpit Gupta has the right idea, but I prefer not to use the panelGrid - the spacing will not match the other fields.  Try using inputField and pageBlockSectionItem like this:

<pre>
<apex:page standardController="Account" sidebar="false">
<apex:form >
<apex:pageBlock >
   <apex:pagemessages />
   <apex:pageBlockSection id="pm" columns="1" title="Dependent Text">
        <apex:inputField value="{!Account.Picklist_Field__c}">
            <apex:actionSupport event="onchange" rerender="pm" />
        </apex:inputField>
        <apex:inputField value="{!Account.Comments__c}" rendered="{!Account.Picklist_Field__c=='Other'}" />
        <apex:pageBlockSectionItem rendered="{!Account.Picklist_Field__c!='Other'}" />
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
 
</apex:page>
</pre>

This should show the Comments field when the picklist is 'Other' and show a blank space when it is anything else.
Arpit Gupta 40Arpit Gupta 40
Hi Glyn,
Yeah you are right. PageblockSectionItem is more useful than panel grid. I already implemented this and its work fine. I have one picklist field "Current_Status__c". If Current Status == "In Project", one dependent text field will appear i.e. TL_PM_Name__c.
Here is my code:
<apex:page standardController="Account"  sidebar="false" showHeader="false" docType="html-5.0"> 
<apex:form > 
	<apex:pageBlock >
		<apex:actionRegion >    <!-- If more than one page block section is their-->
		<apex:pageBlockSection id="pm" title="Work Information" columns="2">
			<apex:inputField value="{!Account.Current_Status__c}"><apex:actionSupport event="onchange" rerender="pm" status="status"/>                                       </apex:inputField>
			<apex:inputField label="PM Email" value="{!Account.TL_PM_Name__c}" rendered="{!Account.Current_Status__c=='In Project'}"/>
		</apex:pageBlockSection>
		</apex:actionRegion>
	</apex:pageBlock >
</apex:form>
</apex:page>

 
Glyn Anderson 3Glyn Anderson 3
sehoop27,  Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved it yourself another way, please post your solution.  Thanks!