Board index JavaServer Faces General Strange AutoComplete behavior

Strange AutoComplete behavior

Components, Ajax Framework, Utilities and More.

Post 01 Nov 2011, 19:56

Posts: 1
Recently I noticed that even tough the forceSelection flag was set to true and no selection was made, the value typed by the user is still being submitted to the form.

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.

<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" />

@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;
   }


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.

Return to General