Find your content:

Search form

You are here

InputField required based on Select list value

 
Share

I have created a select list and an input field. The input field is required based on the select list value "New". The following code is not working for me. Please let me know what am I doing wrong here:

<apex:pageBlockSectionItem >
    <apex:outputLabel value="Request Reason" />
        <apex:outputPanel styleClass="requiredInput" layout="block" id="pwPanel" > 
        <apex:outputPanel styleClass="requiredBlock"/>
        <apex:selectList size="1" value="{!sreason}" label="Request Reason" required="true" style="width: 155px; " >
            <apex:selectOptions value="{!sreasons}" /> 
        </apex:selectList>
    </apex:outputPanel>
</apex:pageBlockSectionItem>
<apex:inputField value="{!Scases.Vendor_Number__c}" required="{!if(sreason== 'New',true,false }"/>

Attribution to: user7292

Possible Suggestion/Solution #1

Whenever you change the value in the picklist you need to re-render the input field so that the reference to variable which keeps the selected value of the picklist is re-evaluated.

You need to put the input field in an apex:outputpanel with an ID and then add apex:actionsupport to the select list:

<apex:outputPanel id="theOutputPanelID">
    <apex:inputField value="{!Scases.Vendor_Number__c}" required="{!if(sreason == New',true,false }"/>
</apex:outputPanel>

Then:

<!-- your code -->
<apex:selectList size="1" value="{!sreason}" label="Request Reason" required="true" style="width: 155px; " >
    <apex:selectOptions value="{!sreasons}" /> 
    <apex:actionSupport event="onchange" rerender="theOutputPanelID" />
</apex:selectList>
<!-- your code -->

This will re-render the output panel which holds the input field and your required attribute will evaluate to true this time.


Attribution to: Boris Bachovski
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30847

My Block Status

My Block Content