PF autocomplete showing wrong label when validation fails
I'm having a weird behaviour with primefaces' autocomplete.
The component works perfectly fine when I submit a form without validation
errors, or with errors on other form fields. However, if validation fails
on the autocomplete, the label is replaced by the item @Id field.
I suspect something is wrong with the converter I'm using. What the
converter does, basically, is get the entity's @Id value and insert the
actual entity in the component's attribute map with the @Id value as its
key.
Here is my xhtml:
<p:autoComplete
id="autoComp"
value="#{action.timeTable}"
completeMethod="#{action.timeTables}"
var="tt"
itemLabel="#{tt.description}"
itemValue="#{tt}"
dropdown="true"
minQueryLength="3"
forceSelection="true"
converter="entityConverter"
size="30"
required="true"
maxResults="10">
<f:validator validatorId="customValidator" />
</p:autoComplete>
And here is my converter code:
@FacesConverter("entityConverter")
public class EntityConverter implements Converter {
@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String
value) {
if (value != null) {
return component.getAttributes().get(value);
}
return null;
}
@Override
public String getAsString(FacesContext ctx, UIComponent component, Object
obj) {
if (obj instanceof String) {
return obj.toString();
}
if (obj != null) {
String id;
try {
id = this.getId(getClazz(ctx, component), obj);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new ConverterException("msg");
}
id = id.trim();
component.getAttributes().put(id, getClazz(ctx,
component).cast(obj));
return id;
}
return null;
}
private Class<?> getClazz(FacesContext facesContext, UIComponent component) {
//get entity's class
}
private String getId(Class<?> clazz, Object obj) throws
NoSuchFieldException, IllegalAccessException {
// get entity's ID value
}
}
No comments:
Post a Comment