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
RDN_LHRRDN_LHR 

Can't get "SelectList" to behave exactly like Salesforce picklist field

I've got two fields side by side, both picklists.  One is a regular Salesforce picklist field, the second is a picklist I'm explicitly tagging so that I can restrict further the values that will eventually be placed in the underlying field on this particular page.

 

I want them to behave exactly the same:  both have a title, both be required, both default to "-None-" option (which errors when left on -None-).  All of this happens with the first field, and none of it with the second: no title, no red mark for required, no enforcement of the required propertly.

 

I've put this:

 

<apex: pageBlockSection id="Section1" columns="2">

 

  <apex:inputField value="{!PSCase.Route_To__c}" required="true"/>

 

 

  <apex:selectList size="1" title="Setup Type" value="{!PSCase.Reason}" required="true"

     <apex:selectOption itemLabel="-None-"/>

     <apex:selectOption itemValue="Trial Setup" itemLabel="Trial"/>

     <apex:selectOption itemValue="Subscription Setup" itemLabel="Subscription"/>

  </apex:selectList>

 

</apex: pageBlockSection>

Message Edited by RDN_LHR on 02-26-2009 05:05 AM
Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

There's no "title" (I think you mean label) next to the field because you did not give it one.  inputField and outputField are the only components that automatically provide their label inside pageBlockSection.  You need to use pageBlockSectionItem, and supply your own label field.  You can use $ObjectType to get the right, translated label.

 

Additionally, selectList is not a salesforce styled component.  InputField is, so when you are inside of a pageBlockSection, it is going to show the red required bar.  selectList will not.  We do not bind our users to the red bar for denoting requiredness unless they are specifically using inputField.  Not all users want salesforce styles as a part of their UI.  If you want to show requiredness in a similar way I would suggest you add your own CSS class (perfectly reasonable to reverse engineer ours--we reserve the right to change our CSS at any time so it's safer if you create the style yourself) around the field to do it.

 

And finally, I haven't had a chance to try this out, but I'm not sure if it considers "-None-" to be a value.  If it does then it will never fail the requiredness check, so you'd have to do your own in your controller.  But since you don't have an itemValue specified I would expect that it would prevent your page from proceeding.  The thing is, since you're not using inputField, again you are not going to get the same look and feel when there is a requiredness error.  Instead it would update your apex: messages or pageMessages component and refresh the page (put one of those on your page and see if the requiredness error shows up this time).  If you want it to look like salesforce you have to do that yourself.

 

Summary: Once you drop out of our salesforce styled components, you lose the magic, salesforce-specific styling and behavior.  And this makes sense because not everyone is creating a salesforce UI. 

All Answers

jwetzlerjwetzler

There's no "title" (I think you mean label) next to the field because you did not give it one.  inputField and outputField are the only components that automatically provide their label inside pageBlockSection.  You need to use pageBlockSectionItem, and supply your own label field.  You can use $ObjectType to get the right, translated label.

 

Additionally, selectList is not a salesforce styled component.  InputField is, so when you are inside of a pageBlockSection, it is going to show the red required bar.  selectList will not.  We do not bind our users to the red bar for denoting requiredness unless they are specifically using inputField.  Not all users want salesforce styles as a part of their UI.  If you want to show requiredness in a similar way I would suggest you add your own CSS class (perfectly reasonable to reverse engineer ours--we reserve the right to change our CSS at any time so it's safer if you create the style yourself) around the field to do it.

 

And finally, I haven't had a chance to try this out, but I'm not sure if it considers "-None-" to be a value.  If it does then it will never fail the requiredness check, so you'd have to do your own in your controller.  But since you don't have an itemValue specified I would expect that it would prevent your page from proceeding.  The thing is, since you're not using inputField, again you are not going to get the same look and feel when there is a requiredness error.  Instead it would update your apex: messages or pageMessages component and refresh the page (put one of those on your page and see if the requiredness error shows up this time).  If you want it to look like salesforce you have to do that yourself.

 

Summary: Once you drop out of our salesforce styled components, you lose the magic, salesforce-specific styling and behavior.  And this makes sense because not everyone is creating a salesforce UI. 

This was selected as the best answer
SaaspertSaaspert
can I use record type to restrict the values shown in the picklist ? 
jwetzlerjwetzler
You can definitely do this.  Unfortunately apex: inputField does not support record types right now so if you're relying on inputField to do the magic for you, it doesn't currently work.  However, you can query for recordTypeId on your object and adjust your selectOptions accordingly.
angusgrantangusgrant

Hi Jill,

 

Thanks for your post most usefull. I am using the <apex:selectlist> visualforce Tag and wish to replicate the behaviour of the standard tag for validation <apex:InputField>. My SelectList is a required field in filling out the form.

 

The moment the validation error that is returned reads the following: 

"j_id0:StdOXCEITemplate:j_id9:j_id37:j_id49:j_id50:org: Validation Error: Value is required."

 

1. My first task will be to create a customise this message to read "You must input a value for thie picklist field division"
2. Will be to replicate the css to make sure the selectList value is displayed with the red left border to show it is required.

3. Will be to make sure that the field is highlited in red in the sameway as it does for the inputfield  with the error message next to field specifying that it is required.

 

Unfortunatly the only one I have any idea on how to implement is the 2nd bullet. Can anyone advise on how this is done?

 

 

<apex:pageBlockSectionItem > <apex:outputLabel value="Organisation" for="org"></apex:outputLabel> <apex:selectList value="{!picklistValue}" required="true" multiselect="1" size="1" id="org"> <apex:SelectOptions value="{!OrganisationNames}"></apex:SelectOptions> <apex:actionSupport event="onchange" rerender="thePageBlock"/> </apex:selectList> </apex:pageBlockSectionItem>

 


 

 

Thanks 

 

Angus

 

ascuccimarraascuccimarra

Hey Jill,

 

Mapping picklist values according to record type using inputField hasn't been implemented yet right?

You mentioned you could do this by querying the object's record type and constructing the List<SelectOption> accordingly.

How would you do this?

I can't seem to find where the map for a picklist values/record type is stored.

 

Thanks

JPlayEHRJPlayEHR

Hi Alvaro - 

 

Here's some of the code I used to get a dynamic list of record types into a selectList dropdown.  An additional requirement for me was that I needed to filter the record types to show only certain ones (only the ones we'd included "- Filter Convention" in the record type name).  

 

 

public List<SelectOption>  getRecType(){

List<SelectOption> options = new List<SelectOption>();
		
RecordType[] rts = 
[select id, name 
from RecordType 
where SobjectType='My_Custom_Object__c' 
and Name like '%- Filter Convention'];
		
Schema.DescribeSObjectResult d = Schema.SObjectType.My_Custom_Object__c; 
		
options.add(new SelectOption('','--SELECT TYPE--'));
		
for(RecordType rt:rts){
			
Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();

Schema.RecordTypeInfo rtById =  rtMapById.get(rt.id);

Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();

Schema.RecordTypeInfo rtByName =  rtMapByName.get(rt.name);

System.assertEquals(rtById,rtByName);
			
options.add(new SelectOption(rt.Id, rt.Name.trim().replaceAll(' - Filter Convention', '')));
				
}  
		     
return options;
	   
}

 

VisualForce Page:

 

 

<apex:pageBlockSectionItem >
     <apex:outputLabel value="Type of Template" for="recordTypes"/>
     <apex:selectList id="recordTypes" value="{!viewDepartmentName}" size="1" required="true">
          <apex:selectOptions value="{!RecType}"/>
     </apex:selectList>             
</apex:pageBlockSectionItem>

 

 

 

 Hope it helps!

 

Marty Y. ChangMarty Y. Chang

Hello, RDN_LHR,

 

This is a very late reply, but still relevant (I think).  I had a similar frustration today when trying to build a standard-looking Visualforce page.  I came up with a hack (http://frombelvideres4thfloor.blogspot.com/2011/11/javascript-hack-to-render-red-bar-for.html) to render the red bar, and fortunately the required attribute took care of the validation.

 

For the "--None--" option, I usually include a getNoneOption method in my Visualforce controller or extension.  The method is very simple and looks like this:

 

public SelectOption getNoneOption() {
    return new SelectOption('', '--None--');
}   // public SelectOption getNoneOption()

 

To use it in a Visualforce page, you can just add it as the first option in a picklist.

 

<apex:selectList>
    <apex:selectOption value="{!noneOption}"/>
</apex:selectList>

 

Also, one immediate change you can make is to put the apex:selectList inside an apex:pageBlockSectionItem.  This will help you get closer to the standard element layout.

 

As for the title, I wasn't quite sure what you were referring to.  If you get this message, would you explain what you mean?  If I understood maybe the knowledge would help me in my own development work.