fred
fred

Reputation: 1822

Wicket AutoCompleteTextField model casting

Looks like the modelobject of the autocompletetextfield is a string, even though it should be an employee, giving rise to exceptions about not being able to cast from string to employee. Why? And how can it be fixed?

        final DefaultCssAutocompleteTextField<Employee> field = new DefaultCssAutocompleteTextField<Employee>("field", new Model<Employee>(model.getObject().getMatch().getSupervisor())) {
            @Override
            protected Iterator<Employee> getChoices(String input) {
                if (Strings.isEmpty(input)){
                    List<Employee> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }
                return supervisorDao.getAutoCompleteCapableSupervisors(input, 6).iterator();
            }
        };

        form.add(field);

        field.add(new AjaxFormSubmitBehavior(form, "onchange") {
            @Override
            protected void onSubmit(AjaxRequestTarget target) {

                //this generates an exception: cant cast from string to employee. why? and how can it be fixed?
                Employee e = supervisorService.findOne(field.getModelObject().getId());

                //do some stuff with the employee and some components

            }

            @Override
            protected void onError(AjaxRequestTarget target) {
                //do nothing
            }
        });

Upvotes: 1

Views: 696

Answers (1)

Christoph Leiter
Christoph Leiter

Reputation: 9345

The javadoc of AutoCompleteTextfield says:

To convert input back into a non-String type you will have to provide a custom IConverter, either by overriding #getConverter(Class) or by setting a suitable IConverter on the application's ConverterLocator.

Upvotes: 3

Related Questions