Skip to content

“Validation Error: Value is not valid” famous validation error, when using custom converters in JSF.

2008 January 29
by Nuwan Bandara

This is one problem i faced when i worked with select-many and select-one menus in Java Server Faces. For any one who have worked with JSF knows that you have to use custom converters in order to populate select-many and select-on menus with ur own data types.

if i elaborate on this a little bit, Select menus are not just there to show simple value-label pairs. you can give directly an object to its value and one of its fields as the label. for an example,

public ArrayList<SelectItem> getLandSelectList() {
if (landSelectList != null) {
return landSelectList;
}
    landSelectList = new ArrayList<SelectItem>();
List<Land> landList = this.getLandList();
for (int i = 0; i < landList.size(); i++) {
        landSelectList.add(new SelectItem(landList.get(i), landList.get(i)
.getLandsName_DE()));
}
return landSelectList;
}

The returning Select list can be taken to a select one or a select many list box like..

<h:selectOneMenu id=”listBoxLand
value=”#{userManagerBean.land}” required=”true”>
<f:selectItems value=”#{userManagerBean.landSelectList}” />
</h:selectOneMenu>
<h:message for=”listBoxLand” />

This component will set the selected land object directly to the backing bean. If you used simple value(some text or the id) – label pair. you should again query for the object from the selected id and save in the backing bean. but in this way that extra trouble will be handled by JSF.

The problem is if you do it just like this with out anything else.. you will get a wired validation error saying “Validation Error: Value is not valid“. This is where you start googling and debugging. Well after some hours of googling..(couldn’t do much debugging because this is a exception thrown by JSF framework) and reading about 10 to 20 forums i found out that the object which is loaded and the object wich was selected will be compared when setting to the backing bean. So if your object’s Class has not overridden the equals method this error message is shown.

So what you have to do is. if you are using your own data Objects for the select menus or in that case for any other JSF tag where you will use converters. You have to override the Equals method. Probably do the comparison with the Id, or with some unique value in that data Object. That’s it.. The problem solved. In my case the equals methods looks like

// overridden equals method
public boolean equals(Object obj) {
if (!(obj instanceof Land)) {
return false;
}
Land land = (Land) obj;

return (this.id == land.id);

}

Yeah hope this will be useful to some one.. !! I will write another post on how to use Java Collections when working with Select-Many menus.

28 Responses leave one →
  1. February 7, 2008

    Hey Nuwan,

    I guess you just saved me some hours of googling ;)

    Thanks mate!
    Lorenz

  2. Nuwan Bandara permalink*
    February 7, 2008

    Am glad to hear mate..
    cheers !!

  3. Trudko permalink
    February 8, 2008

    I am still gettin error even when I put as a object new Long(long) value but when I put String except Long it work properly

  4. Nuwan Bandara permalink*
    February 8, 2008

    is this in the equals method, or in the converter ?

  5. David Atkins permalink
    May 9, 2008

    You just saved me hours of head scratching! Cheers!

  6. May 16, 2008

    It’s great!
    Thank’s, you save me:D:D!

  7. Pradeep permalink
    May 20, 2008

    Thanks a lot for your suggestion. Adding the equals method fixed the issue :)

  8. Fahim permalink
    June 3, 2008

    Thank you very much. Saved my life!

  9. Jörg permalink
    June 6, 2008

    thanks a lot. Now I can sleep well to night!

  10. Stuart permalink
    June 6, 2008

    Thanks very much for that. I’m amazed that this isn’t in any of the reference / tutorial material I have read.

  11. Rahul permalink
    June 12, 2008

    Can some one please explain whose overide method is this equals(Object obj) method.

  12. Nuwan Bandara permalink*
    June 12, 2008

    yes it is the Objects’ equals method, since all classes are inheriting that.

    cheers!

  13. Rahul permalink
    June 13, 2008

    Hi Nuwan, I could not explain my last question. I am facing the same problem as discussed above. Just a small difference, that i am adding strings to SelectItem label and value both:
    landSelectList.add(new SelectItem(String, String));

    So do i still need to override equals method. Then where do i write the equals method as i dont have any object.
    Some other forums discussed about using converters. But in my case i dont think i need to write converter in case i m using simple string.

    Please help me.

  14. Nuwan Bandara permalink*
    June 13, 2008

    if your value is also a string you don’t need a converter (Nor the over riding), the component should work fine, its simply like when you add the items in the jsp page.

    anyhow this error message does not come only for this error. but this is the most common one as i found out. What I would suggest is, do some debugging. example, just add about one or two select items (With out iterating), and try, and then also try adding the items from the jsp code (not from the been)





    the above is the simplest way. Try that also, after that instead of this put the string values for the been with out any converter. and technically it should work

    good luck

  15. lalotor permalink
    July 9, 2008

    Hi Nuwan, your post has been of great help for me. Thanks in advance.

    Elkin

  16. Reylim permalink
    August 15, 2008

    Excelent!!!!!, this tip can save my life, thanks.

  17. Kamlesh permalink
    October 14, 2008

    Hi,
    where we have to write the code for equals method.
    Kamlesh

  18. Luiz permalink
    January 16, 2009

    I will say this in the most heterosexual way posible:
    I love you and if i was a girl i’d love to have your babies.

    thx man

  19. asmweb permalink
    January 23, 2009

    Thank you very much… saved my days :)

  20. March 12, 2009

    thanks a lot my friend, iwas about to be crazy by this problem

  21. Veera permalink
    April 6, 2009

    Hi, Mine is the similar issue, but the only differnece is i am retrieving from the db as SelectItem itself. How can i override the equals method. in my case.

  22. Inca permalink
    July 3, 2009

    Hi, mate, very useful post! Thank-a-lot:)

    Would like to add a bit for those who use Hibernate entities with their equals/hashcode methods overriden: depending on the logic of your converter you may face the problem that an entity is being compared with it’s proxy (e.g. object with class “Realm” against an object with class “Realm_$$_javassist_34535″). So if you override equals() and hashcode() in your entity class just make sure they call getter method instead of accessing the field (e.g. do “this.getId() == that.getId()” instead of “this.id = that.id”). This way lazy initializer will load properties you are using in comparison.

    Cheers :)

  23. August 27, 2009

    Thank you!!! for usefull post

  24. chris permalink
    December 22, 2009

    Hi saved me hours too. Done it before and forgot about it. Thanks to Google and thanks to you !

    Merry Xmas

  25. Tidza permalink
    April 8, 2010

    Thank u so much Nuwan, your post was very helpful in my case because I was having problems with drop down menus but the moment I used the equals method, everything is sorted out.

  26. Jonathan permalink
    June 13, 2010

    Thanks a lot. Couldn’t understand why a regular Converter wasn’t working. Got caught by a cut and paste in the Hibernate class.

  27. nbk permalink
    March 9, 2011

    Thank you! I can sleep at nights after 2 days of nightmare. Now I wonder why I didn’t google this before.

  28. Himalay permalink
    March 9, 2011

    Excellent..worked like a charm for me

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS