0

I'm having some trouble using p:autoComplete properly, I am trying to let a user search based on a text field of an object. Currently I have the following:

<p:autoComplete forceSelection="true" value="#{answer.destinationQuestion}"
                completeMethod="#{editorView.completeText}"
                var="destinationQuestion" itemLabel="#{destinationQuestion.questionText}"
                itemValue="#{destinationQuestion}"/>
public List<Question> completeText(String query) {
        List<Question> ret = new ArrayList<Question>();
        
        for(Section section : this.survey.getSections()) {
            for(Question question : section.getQuestion()) {
                if(question.getQuestionText() != null && question.getQuestionText().contains(query)) {
                    ret.add(question);
                }
            }
        }
        
        return ret;
    }

My expectation with this code is that the autoComplete would let a user select a single Question instance, but once a value is selected from the autoComplete and an update event is triggered, I receive the following exception:

java.lang.IllegalArgumentException: Cannot convert Question@228bb9f7 of type class java.lang.String to class Question

It has been suggested that I use a converter, but I am confused why the value selected is being treated as a String rather than my Question object, since itemValue is defined to be a Question object.

Edit: This is on PrimeFaces 6.2

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Gumpf
  • 197
  • 7
  • You are adding a `String question` to the `List ret` – Omar_Hafez May 16 '22 at 13:08
  • @Gumpf Please add the `Question` Code here – Omar_Hafez May 16 '22 at 13:10
  • 2
    Does this answer your question? [Conversion Error setting value for 'null Converter' - Why do I need a Converter in JSF?](https://stackoverflow.com/questions/4734580/conversion-error-setting-value-for-null-converter-why-do-i-need-a-converter) – Jasper de Vries May 16 '22 at 15:19
  • Yes, many thanks, I had not considered objects return by EL's to be treated as strings, but I suppose it makes sense. The omnifaces.ListConverter also seems to work for conversion. – Gumpf May 16 '22 at 15:36

1 Answers1

0

Based on your code, what I can tell you is, you are missing a custom converter class. You should use it like converter="#{questionConverter}".

For further reference you can see this

Asgar
  • 1,920
  • 2
  • 8
  • 17