Using Enum property for Dropdown or RadioButton Group

Hello,

I am trying to achieve the following:

  1. populate a single-select dropdown or a radio button group with all possible values of an Enum.
  2. Bind the value of the component to a Java bean proptery of type Enum.

This code is used to populate the dropdown:

public List<SelectItem> getQuotationTypeItems() {
        QuotationTypeEnum[] values = QuotationTypeEnum.values();
        List<SelectItem> list = new ArrayList<SelectItem>(values.length);
        for (QuotationTypeEnum value : values) {            
            list.add(new SelectItem(value.name(), value.getDisplayValue()));            
        }
        
        return list;
    }

The getters and setters of the property the component is bound to are as follows:

public QuotationTypeEnum getQuoteType() {
	return this.quoteType;
}

public void setQuoteType(QuotationTypeEnum quoteType) {
	this.quoteType = quoteType;
}

where QuotationTypeEnum is a java enum.

The dropdown is populated correctly but the selected value is not bound. I receive the error message: {0}: Validation Error: Value is not valid

Is CAF not supporting converting enums - that would be strange because JSF supports enum conversion out of the box.

Any help is appreciated.

Regards,
Mathias

Hi Mathias,
Since you are mapping the value of the dropdown/radio-button-group to a QuotationTypeEnum you need a small correction in your list of options:

public List getQuotationTypeItems() {
QuotationTypeEnum[] values = QuotationTypeEnum.values();
List list = new ArrayList(values.length);
for (QuotationTypeEnum value : values) {
list.add(new SelectItem(value, value.getDisplayValue()));
}
return list;
}

Otherwise, the list of options would contain a String as value, not a QuotationTypeItem.
Hope this helps,
Javier