Tuesday, September 27, 2011

Make Select List Required On Visualforce Page in Apex Salesforce


Hello guys ,
If we are creating a select list on Visualforce page its not possible to make it required by setting its attribute required="true".

So we have to alter some code in that.
For example :

<apex:selectList value="{!countries}"  multiselect="false" size="1">

            <apex:selectOptions value="{!items}"/>

</apex:selectList>


items - > list of your selected items
countries - > variable which hold the selected value.

required="true" - > Did not work in this way ..


Now I am writing some think like this..

<apex:form >



<apex:pageBlock >

        <apex:pageBlockSection >

      <apex:outputPanel styleClass="requiredInput" layout="block" id="pwPanel" >

   <apex:outputPanel styleClass="requiredBlock"/>
                    <apex:selectList value="{!countries}" multiselect="false" size="1">

                              <apex:selectOptions value="{!items}"/>

                       </apex:selectList>

    </apex:outputPanel>

        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>

        </apex:pageBlockSection>

        </apex:pageBlock>

    </apex:form>



Now this select list values will display as required even no need to put the required attribute.

Cheers.....................

4 comments: