You need to sign in to do that
Don't have an account?
Crazy trouble with multiselect selectList
OK, I must be going crazy because I think this was working yesterday... I need to have a VF page with a
multiselect list, which I can't seem to get working. Here's the simplest case I've been able to boil it
down to.
VF Page:
<apex:page controller="TestController" sidebar="false" showHeader="false"> <apex:pageBlock title="Values" rendered="true" id="Pageid"> <apex:form > <apex:pageBlockSection columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Test: " for="pickTest"/> <apex:selectList value="{!Values}" size="4" id="pickTest" multiselect="true" > <apex:selectOptions value="{!ValueOptions}"/> </apex:selectList> </apex:pageBlockSectionItem> <apex:commandButton action="{!testIt}" value="Test It!"/> </apex:pageBlockSection> </apex:form> </apex:pageBlock> </apex:page>
Controller:
public class TestController { private String [] m_lstVals; public void setValues (String [] lstVals) { m_lstVals = lstVals; } public String [] getValues () { return m_lstVals; } public List<SelectOption> getValueOptions() { List<selectoption> lstOptions = new List<SelectOption>(); lstOptions.add(new SelectOption ('one', 'one')); lstOptions.add(new SelectOption ('two', 'two')); lstOptions.add(new SelectOption ('three', 'three')); return lstOptions; } public PageReference testIt () { if (m_lstVals == null) { System.debug ('No values'); return null; } for (String s : m_lstVals) { System.debug ('Selected value: ' + s); } return null; } public static testmethod void testController () { TestController tc = new TestController (); List<SelectOption> so = tc.getValueOptions(); } }
When I select 'one' and 'two' and press the 'test It' button, I get the following in the debug log:
Conversion Error setting value 'one two' for '#{Values}'.
When I set 'multiselect' to 'false' (and change the accessor methods to just Strings) everything works
correctly. It must be something really silly/simple here! Could someone take a look and point out my
mistake?
Thanks in advance!
Try initializing your list first; I changed the first line of your code to the following and it is working for me now.
private String [] m_lstVals = new String[]{};
-- Matt
All Answers
Try initializing your list first; I changed the first line of your code to the following and it is working for me now.
private String [] m_lstVals = new String[]{};
-- Matt
Hi Matt,
Thanks--that was it! The bitter irony is that I'd actually seen this 'fix' before I posted, tried it, and it didn't seem to work. I think that was because I was testing with my 'real' app, rather than the much simplified example. In that 'real' app I had multiple fields declared that way, and changing only one to the approach that you recommned didn't seem to do the trick.
Interestingly, I think this used to work before some new fields in that controller got added that declarres String lists as:
List<String>
rather than:
String []
In any case, thank you very much for your help!
thanks, this guided me to the roots of my similar issue. I had actually just automatic getter setter property which did not seem to work with multiple select list.