You need to sign in to do that
Don't have an account?

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>
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
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.
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
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
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).
VisualForce Page:
Hope it helps!
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:
To use it in a Visualforce page, you can just add it as the first option in a picklist.
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.