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
sfdc@isha.ax1814sfdc@isha.ax1814 

Make a field required dynamically based on picklist value using Visualforce

Hi Team,

I have below vf page. I want to make comment field required only when picklist vaue is  'Other - explain'.

can you please help me


<apex:page showHeader="false" action="{!InitMethod}" cache="false" controller="AH_HN_PhysicianAcceptnReject_CLS" tabStyle="Case">
    <apex:includeScript value="/soap/ajax/36.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/36.0/apex.js"/>
    <apex:includeLightning />
    
    <head>
        <apex:slds />
        <div class="slds-align_absolute-center">
            <apex:image url="{!$Resource.AH_Physician_Expert_Template_Logo}" />
        </div>
    </head>
    
    <apex:form >
        <div class="slds-align_absolute-center">
            
            <apex:outputText rendered="{!ThanksmsgAccpt}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for accepting, the case has now been added to your dashboard.  Please log into the physician portal to begin your review.</span>
            </apex:outputText> 
            
            <!--
<apex:outputText rendered="{!ThanksmsgRejec}">
<span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for rejecting the case.<br/> </span>
</apex:outputText>
-->
            
            <apex:outputText rendered="{!ThanksmsgRejec}">
                <apex:pageBlock >
                    <!-- span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for rejecting the case.<br/><br/> </span -->
                    
                    <p>Please select the reason:</p>
                    <apex:selectList id="Pleaseselectthereason" size="1" value="{!caseRejectReason}">
                        <apex:selectoption itemLabel="Out of office-cannot complete due to existing workload" itemValue="Out of office-cannot complete due to existing workload"></apex:selectoption>
                        <apex:selectoption itemLabel="I recommend another physician more suitable for this case.Enter Name in Free Text Box" itemValue="I recommend another physician more suitable for this case.Enter Name in Free Text Box"></apex:selectoption>
                        <apex:selectoption itemLabel="Other - explain" itemValue="Other - explain"></apex:selectoption>
                    </apex:selectList>
                    <br/>
                    
                    <p>Comments:</p>
                    <apex:inputTextarea label="Comments" style="width:550px;height:100px" id="comments" value="{!caseRejectComment}"/> 
                    
                    <br/><br/>
                    
                    <div align="center" draggable="false" >
                        <apex:commandButton action="{!saveRejectInfo}" value="Save"/>
                    </div>
                    
                </apex:pageBlock>
            </apex:outputText>
            
            <apex:outputText rendered="{!rejectreasonmessage}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for rejecting the case.</span>
            </apex:outputText>
            
            
            
            
            <apex:outputText rendered="{!Assignedtoothers}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">This case has already been assigned to some other physician.</span>
            </apex:outputText>
            <apex:outputText rendered="{!AlreadyAccepted}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">You have already accepted the case.</span>
            </apex:outputText>
            <apex:outputText rendered="{!AlreadyRejected}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">You have already rejected the case.</span>
            </apex:outputText>
        </div>   
        
        
    </apex:form>
</apex:page>

Regards,
Isha
ANUTEJANUTEJ (Salesforce Developers) 
Hi Isha,

>> https://blog.enree.co/2019/04/salesforce-back-to-basics-how-to-make-a-field-required-based-on-selected-picklist-value.html

The above article has a step-by-step implementation of making a field required based on selected picklist value by using a validation rule.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
sfdc@isha.ax1814sfdc@isha.ax1814
Hi ANUTEJ,

Thanks for your reply.

Iam looking for the change in  Vfpage directly as these are the strings passing from controller.

Regards,
Isha
ANUTEJANUTEJ (Salesforce Developers) 
>> https://salesforce.stackexchange.com/questions/161604/rerendering-conditionally-required-fields-in-vf-how-to-make-field-unrequired

The above link has two ways to make a field conditionally required you can try checking the mentioned solutions.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
sfdc@isha.ax1814sfdc@isha.ax1814
Hi ANUTEJ,

I tried below code. but not working for me. Cam you please help me to fix this issue. This is high priority change.

<apex:page showHeader="false" action="{!InitMethod}" cache="false" controller="AH_HN_PhysicianAcceptnReject_CLS" tabStyle="Case">
    <apex:includeScript value="/soap/ajax/36.0/connection.js"/>
    <apex:includeScript value="/soap/ajax/36.0/apex.js"/>
    <apex:includeLightning />
    
    <head>
        <apex:slds />
        <div class="slds-align_absolute-center">
            <apex:image url="{!$Resource.AH_Physician_Expert_Template_Logo}" />
        </div>
    </head>
    
    <apex:form >
        <div class="slds-align_absolute-center">
            
            <apex:outputText rendered="{!ThanksmsgAccpt}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for accepting, the case has now been added to your dashboard.  Please log into the physician portal to begin your review.</span>
            </apex:outputText> 
            
            <!--
<apex:outputText rendered="{!ThanksmsgRejec}">
<span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for rejecting the case.<br/> </span>
</apex:outputText>
-->
            
            <apex:outputText rendered="{!ThanksmsgRejec}">
                <apex:pageBlock >
                    <!-- span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for rejecting the case.<br/><br/> </span -->
                    
                    <p>Please select the reason:</p>
                     <apex:actionRegion >
                    <apex:selectList id="Pleaseselectthereason" size="1" value="{!caseRejectReason}">
                        <apex:selectoption itemLabel="Out of office-cannot complete due to existing workload" itemValue="Out of office-cannot complete due to existing workload"></apex:selectoption>
                        <apex:selectoption itemLabel="I recommend another physician more suitable for this case.Enter Name in Free Text Box" itemValue="I recommend another physician more suitable for this case.Enter Name in Free Text Box"></apex:selectoption>
                        <apex:selectoption itemLabel="Other - explain" itemValue="Other - explain"></apex:selectoption>
                        <apex:actionSupport event="onchange" rerender="comments"/>
                    </apex:selectList>
                    </apex:actionRegion>
                    <br/>
                    
                    <p>Comments:</p>
                    <apex:inputTextarea label="Comments" style="width:550px;height:100px" id="comments" value="{!caseRejectComment}" required="{!If(CONTAINS(caseRejectReason,"Other - explain"),"true","false")}"/> 
                    
                    <br/><br/>
                    
                    <div align="center" draggable="false" >
                        <apex:commandButton action="{!saveRejectInfo}" value="Save"/>
                    </div>
                    
                </apex:pageBlock>
            </apex:outputText>
            
            <apex:outputText rendered="{!rejectreasonmessage}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">Thank you for rejecting the case.</span>
            </apex:outputText>
     
            <apex:outputText rendered="{!Assignedtoothers}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">This case has already been assigned to some other physician.</span>
            </apex:outputText>
            <apex:outputText rendered="{!AlreadyAccepted}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">You have already accepted the case.</span>
            </apex:outputText>
            <apex:outputText rendered="{!AlreadyRejected}">
                <span style="font-size:20px;font-weight:bold;" class="slds-m-top_xx-large">You have already rejected the case.</span>
            </apex:outputText>
        </div>   
        
        
    </apex:form>
</apex:page>

Regards,
Isha