hello123
hello123

Reputation: 951

Primefaces AutoComplete validator is validating the wrong value

I've got an autocomplete with a validator - if I submit a query of 'AAA' to the autocomplete, then I get a list of suggestions beginning with 'AAA'. I selected 'AAA001', then the validator is called, but in the validator the value is 'AAA' when I'm expected 'AAA001'.

Here is my code:

<p:autoComplete id="codeAutoComplete"
   completeMethod="#{BEAN.autoCompleteCode}"
   minQueryLength="3" forceSelection="true"
   value="#{OBJECT.code}"
   validator="#{BEAN.validateCode}" >
   <f:attribute name="object" value="#{OBJECT}" />
   <p:ajax listener="#{BEAN.onCodeSelect}"
       update="form"/>
</p:autoComplete>

code is simply a String with a getter and setter and my validator:

public void validateCode(FacesContext ctx, UIComponent component, Object value) {
    String code = (String) value;
    ...
}

And instantly code = 'AAA' rather than the expected 'AAA001'. I added the object attribute just to ensure that 'AAA001' wasn't already set on the object, but as expected it wasn't.

Is there anything I can do to get the selected value rather than the submitted one?

Thanks

Upvotes: 1

Views: 3009

Answers (2)

Mike Balthasar
Mike Balthasar

Reputation: 173

I had the same issue and I solved it by simply excluding the autocomplete from the updated area. So if you want to update the whole form without the autocomplete part, here is how to do it: How to exclude child component in ajax update of a parent component?

Upvotes: 0

That Guy Lionel
That Guy Lionel

Reputation: 121

AUTOCOMPLETE validator function (as per the Primefaces documentation) - A method expression referring to a method validating the input.

The object that you're passing into the "Validator" validates the inserted value and not the selected value, as this function is completed before any other function per the code provided.

IMAGE - A quick drawing explaining the validator's purpose

I'd rather suggest that you move the logic that you want to apply for the 'AAA001' to the 'BEAN.onSelect' listener instead, as this applies to the value object selected as apposed to the inserted value object (should the event be set accordingly).

I hope this helps you in resolving the issue. Best of luck!

Upvotes: 2

Related Questions