Tuesday, 24 June 2014

How to use required attribute in picklist as like salesforce standard?

There is an attribute called Required which can be used in almost all visual-force component.If we use that attribute then red color mark will be appended with the field and if we don't enter any value ,try to save,then it shows one error(You must enter some value) just below to that field. 

For example:-
<apex:inputField value="{!account.Industry}" id="field1" required="true"/> 

save image Here Industry is a standard pick-list, if we write required = true, red mark is coming,however if we are making pick-list in controller and displaying using <apex:selectlist> and <apex:selectoptions> then red won't be displayed. 
<apex:selectList label="Picklist" multiselect="false" size="1" required = "true">
               <apex:selectOptions value="{!options }">
                  </apex:selectOptions>

 </apex:selectList>

options = new list<SelectOption>();
        options.add(new SelectOption('-None-','-None-'));
        options.add(new SelectOption('A','A'));
        options.add(new SelectOption('b','b'));
        options.add(new SelectOption('c','c'));
        options.add(new SelectOption('d','d'));


It will display like below image

save image 
How can we display like standard pick-list?
Below is some code for this.
<apex:pageBlockSection id="someArea">
          <apex:pageBlockSectionItem >
               <apex:outputLabel value="picklistLabel"/>
                 <apex:outputPanel layout="block" styleClass="requiredInput">
                     <apex:outputpanel layout="block" styleClass="requiredBlock"/>
                     <apex:selectList label="Picklist" multiselect="false" size="1" >
                            <apex:selectOptions value="{!options }"/>
                   </apex:selectList>
                </apex:outputPanel>
                </apex:pageBlockSectionItem>

</apex:pageBlockSection>
save image

No comments: