Reputation: 3974
I have a comboBox on an xpage bound to a Number field. The code used to populate the SelectItem values for the combo is in a managed bean, looking like this:
/*
* Returns last year, current year and next year as List<SelectItem>
* For use with comboBox selection values
*/
public List<SelectItem> getYearSelectItems() {
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
List<SelectItem> selectItems = new ArrayList<SelectItem>();
selectItems.add(new SelectItem(new Integer(thisYear - 1), new Integer(thisYear - 1).toString()));
selectItems.add(new SelectItem(new Integer(thisYear), new Integer(thisYear).toString()));
selectItems.add(new SelectItem(new Integer(thisYear + 1), new Integer(thisYear + 1).toString()));
for(int i = 1; i < selectItems.size(); i ++) {
System.out.println(new Integer(i).toString() + ": " + selectItems.get(i).getValue().getClass());
}
return selectItems;
}
As you can see I'm printing the class names for the SelectItem values to the console for debugging. The classname listed in the console is java.lang.Integer, so that part is definitely right.
The combobox is using a number converter to match the field in the form, integer only.
The problem is that with this configuration the validation fails.
I know there is another question on this site that addresses a similar problem, but mine has a different angle since I'm using a bean. In the other question it was suggested that this worked in 853 but not in 9. If so; Is this a bug? And does the bug have SPR?
Please help me shred some light on this.
Thanks, Ove
Upvotes: 3
Views: 2266
Reputation: 3974
It turns out the <xp:convertNumber>
misinterprets SelectItem(<Integer>, <String>)
for some strange reason. Changing to Double resolved my problem.
Code:
/*
* Returns last year, current year and next year as List<SelectItem>
* For use with comboBox selection values
*/
public List<SelectItem> getYears() {
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
List<SelectItem> years = new ArrayList<SelectItem>();
years.add((new SelectItem(new Double(thisYear - 1), new Integer(thisYear - 1).toString())));
years.add(new SelectItem(new Double(thisYear), new Integer(thisYear).toString()));
years.add(new SelectItem(new Double(thisYear + 1), new Integer(thisYear + 1).toString()));
}
return years;
}
Upvotes: 2
Reputation: 1640
The Number Text box does not use Integer values also if you use integerOnly=true
in the converter.
You can verify this by binding such a NumberField to a Bean using Object as variable type and add sysout(newValue.class) to the getter&setter.
If you use Integer as vartype you will get an internal Invalid Argument exception when Setting the value to the Numberfield. With double you should be save and also pass the Validation.
Here how i would use the Bean (code is ugly):
private static Map<String,Integer> years;
private double selectedYear = 2013;
static {
Integer thisYear = Calendar.getInstance().get(Calendar.YEAR);
years = new LinkedHashMap<String, Integer>();
years.put(new Integer(thisYear - 1).toString(),new Integer(thisYear - 1));
years.put((thisYear).toString(),new Integer(thisYear));
years.put(new Integer(thisYear + 1).toString(),new Integer(thisYear + 1));
}
public void yearChanged(ValueChangeEvent e){
System.out.println("event Start " + selectedYear);
selectedYear = years.get(e.getNewValue());//Set new Value to field
System.out.println("event End " + selectedYear);
}
public Object[] getYearInMap() {//select Items
return this.years.keySet().toArray();
}
//Binding for the NumberField
public void setSelectedYear(double selectedYear) {
this.selectedYear = selectedYear;
}
public double getSelectedYear() {
return selectedYear;
}
My Combobox and my Field, atm the inputText is readonly otherwise it would get a value from the Combobox and the user.
<xp:comboBox
loaded="true"
id="comboBox2"
valueChangeListener="#{XPageBean.yearChanged}"
defaultValue="#{XPageBean.selectedYear}">
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:XPageBean.getYearInMap();}]]></xp:this.value>
</xp:selectItems>
<xp:eventHandler
event="onclick"
submit="true"
refreshMode="partial"
refreshId="refresh">
<xp:this.action><![CDATA[#{javascript://}]]></xp:this.action>
</xp:eventHandler>
</xp:comboBox>
<xp:panel id="refresh">
<xp:inputText
id="txt_out3"
value="#{XPageBean.selectedYear}"
defaultValue=""
readonly="true">
<xp:this.converter>
<xp:convertNumber
type="number"
integerOnly="true">
</xp:convertNumber>
</xp:this.converter>
</xp:inputText>
<xp:text id="txt_out2"
value="#{XPageBean.selectedYear}">
<xp:this.converter>
<xp:convertNumber
type="number"
integerOnly="true">
</xp:convertNumber>
</xp:this.converter>
</xp:text>
</xp:panel>
Upvotes: 0