I don't know if this is how it's supposed to be, but I could not find a workaround for the problem.
Below are to codes for the xhtml, the converter and the completion method.
Code: Select all
<p:autoComplete id="dataEntryTechnician"
value="#{currentForm.offlineDataEntry.technician}" var="technician"
completeMethod="#{technicianCompletionView.complete}"
itemLabel="#{technician.name} (#{technician.id})"
itemValue="#{technician}" converter="#{userConverter}"
disabled="#{readOnly}" size="50"
forceSelection="true" />Code: Select all
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Integer id;
ElsaUser result;
try {
id = Integer.parseInt(value);
result = userService.loadLazyUserRoles(userService.findById(id));
} catch (Exception e) {
result = null;
}
return result;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
String result = "";
if (value != null) {
result = ((ElsaUser) value).getId().toString();
}
return result;
}Code: Select all
public List<ElsaUser> complete(String input) {
List<ElsaUser> result = new LinkedList<ElsaUser>();
Integer userId;
try {
userId = Integer.valueOf(input);
result.add(userService.findById(userId));
} catch (NumberFormatException e) {
// empty !!!
} catch (DataNotFoundException e) {
// empty !!!
}
try {
result.addAll(userService.findByName(input));
} catch (DataNotFoundException e2) {
// empty !!!
}
return result;
}When the user types a valid ID into the autoComplete but does not select any of the suggestions, instead of submitting an empty string, the typed ID is passed to the converter, which finds the user.
Using Primefaces 2.2.1 on Jboss 6, JSF 2.0.