0

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 }"/>

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.