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
NasirNasir 

How to make a field REQUIRED in VF page when i am using "selectlist"

Hi ,

 

I want to make a field required in VF page and it uses Selectlist.As there is one more condition for which the required is working on.Please see the code below.

<apex:pageBlockSectionItem labelStyle="width: 300px" dataStyle="width: 200px" > 
                         <apex:outputLabel value="Gas Commission Type"  /> 
                         <apex:selectList value="{!SaleRec.Commission_Type_Gas__c}" size="1"  id="NewSupplierGasCommissionType" required="{!SaleRec.Status__c == 'Submited Sale'}">
                                <apex:selectOptions value="{!GasCommissionType}"/> 
                                <apex:actionSupport event="onchange" oncomplete="GasCommissionType('{!$Component.NewSupplierGasCommissionType}');" rerender="NewSupplierProductGasTypeTemp"/>
                            </apex:selectList> 
                      </apex:pageBlockSectionItem>

 All i want to do is to make the field "Commission_type_gas__C" required and to display it in the red mark in UI.As required field is used there but it is on a condition.

 

Please help

Nasir

 

Ispita_NavatarIspita_Navatar

Hi,

Let me understand the issue faced by you. In the code snippet below (which you have shared):-

 

<apex:selectList value="{!SaleRec.Commission_Type_Gas__c}" size="1"  id="NewSupplierGasCommissionType" required="{!SaleRec.Status__c == 'Submited Sale'}">

 

The required attribute should be set only it the condition - {!SaleRec.Status__c == 'Submited Sale'} is fulfilled. Is that what you are trying to achieve?

Well then this is the response :-

The red bar and salesforce error messaging only applies to inputField which is a standard salesforce component that inherits salesforce formatting and behavior.  There are many many posts about this on the forums.  It would not be appropriate for us to dictate how your requiredness rules work if you are not using inputField, so therefore if you must use selectList then you must handle requiredness yourself.  The only thing that required="true" will do is add a message to your messaging component and prevent the form from successfully submitting.

 

So probably you could have the red bar in a div next to your selectlist and show/hide it conditionally.

 

 

<tr style="display:{!IF( condition to hide , 'None', '')}">

  <td><img src="path of red bar image">

 </tr>

 

Did this answer your question? If not, let me know what didn't  work, or if so, please mark it solved.


ColinKenworthyColinKenworthy

You can do it in VF with output panels (they generate DIV tags), or you could try coding the DIV tags yourself.

 

 

<apex:pageBlockSectionItem labelStyle="width: 300px" dataStyle="width: 200px" > 
<apex:outputLabel value="Gas Commission Type" />
<apex:outputPanel layout="block" styleClass="requiredInput">
<apex:outputPanel layout="block" styleClass="requiredBlock"></apex:outputPanel>
<apex:selectList value="{!SaleRec.Commission_Type_Gas__c}" size="1" id="NewSupplierGasCommissionType" required="{!SaleRec.Status__c == 'Submited Sale'}">
<apex:selectOptions value="{!GasCommissionType}"/>
<apex:actionSupport event="onchange" oncomplete="GasCommissionType('{!$Component.NewSupplierGasCommissionType}');" rerender="NewSupplierProductGasTypeTemp"/>
</apex:selectList>
  </apex:outputPanel>
</apex:pageBlockSectionItem>