I have an enum whose code is like this -
public enum COSOptionType {
NOTAPPLICABLE,
OPTIONAL,
MANDATORY;
private String[] label = { "Not Applicable", "Optional", "Mandatory"};
@Override
public String toString() {
return label[this.ordinal()];
}
public static COSOptionType getCOSOption(String value) {
int ivalue = Integer.parseInt(value);
switch(ivalue) {
case 0:
return NOTAPPLICABLE;
case 1:
return OPTIONAL;
case 2:
return MANDATORY;
default:
throw new RuntimeException("Should not get this far ever!");
}
}
}
I have the converter to convert the enum type
public class COSEnumConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent comp, String value) {
return COSOptionType.getCOSOption(value);
}
public String getAsString(FacesContext context, UIComponent comp, Object obj) {
if (obj instanceof String) {
return (String) obj;
}
COSOptionType type = (COSOptionType) obj;
int index = type.ordinal();
return ""+index;
}
}
The view looks like this
<h:selectOneMenu value="#{controller.type}" id="smoking">
<f:selectItems value="#{jnyController.choices}" />
</h:selectOneMenu>
Here is the code for create choices
private List<SelectItem> createChoicies() {
List<SelectItem> list = new ArrayList<SelectItem>();
for (COSOptionType cos : COSOptionType.values()) {
SelectItem item = new SelectItem();
item.setLabel(cos.toString());
item.setValue("" + cos.ordinal());
list.add(item);
}
return list;
}
I do not understand why this would throw "validation error" all the time ? I can debug and see that the converter is working fine.
NOTE: I am using JSF 1.1