I pretty much found an answer here, but I still will explain in this post because the reference answer does not address this particular issue directly :
The object that was used in the code example didn't seem to go through any kind of conversion. Although I could previously use objects without a converter, they were actually enum
and this one is not. So it seems like anytime I need a custom object that is not an enum, I need to create a converter for it (either serialize the data manually into a string or simply use an id and recover the data from a list of the possible objects)
Here's what worked for me :
<p:selectOneMenu id="selectOption"
converter="optionConverter"
required="true"
requiredMessage="#{bundle.mandatory_field_message}"
rendered="#{myBean.previousOptionSelected eq 'Correct Option'}"
value="#{myBean.selectedOption}">
<c:if test="#{myBean.optionsList.size() ne 1}">
<f:selectItem itemLabel="#{bundle.default_select}" itemValue="" />
</c:if>
<f:selectItems value="#{myBean.optionsList}" var="opt"
itemValue="#{opt}" itemLabel="#{opt.description}" />
<f:ajax event="change" render="msgSelectOption" />
</p:selectOneMenu>
<p:messages id="msgSelectOption" for="selectOption" display="text" />
with the following converter:
@FacesConverter(forClass = myPackage.OptionClass.class, value="optionconverter")
public class OptionConverter implements Converter{
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (submittedValue != null && !submittedValue.trim().equals("")) {
for (OptionClass opt : OptionListingService.getEntries()) {
if (submittedValue.equalsIgnoreCase(opt.getUniqueID())) {
return opt;
}
}
}
return null;
}
public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return ((OptionClass)value).getUniqueID();
}
}
}
Also note that I changed the value of itemValue="null"
to itemValue=""
in this item:
<f:selectItem itemLabel="#{bundle.default_select}" itemValue="" />
It was necessary because the converter would interpret the value as a String, thus would be trying to convert a string value with the id
equal to "null"
. The code would still have worked, but I like (and it's also a better pratice) to manipulate data as clean as possible.
Thank you Dusan Kovacevic for pointing a possible Converter issue.