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

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.

26 Comments

  1. Hey Nuwan,

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

    Thanks mate!
    Lorenz

  2. Nuwan Bandara says:

    Am glad to hear mate..
    cheers !!

  3. Trudko says:

    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 says:

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

  5. David Atkins says:

    You just saved me hours of head scratching! Cheers!

  6. Wagner says:

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

  7. Pradeep says:

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

  8. Fahim says:

    Thank you very much. Saved my life!

  9. Jörg says:

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

  10. Stuart says:

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

  11. Rahul says:

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

  12. Nuwan Bandara says:

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

    cheers!

  13. Rahul says:

    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 says:

    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 says:

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

    Elkin

  16. Reylim says:

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

  17. Kamlesh says:

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

  18. Luiz says:

    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 says:

    Thank you very much… saved my days :)

  20. monaam says:

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

  21. Veera says:

    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 says:

    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. Thank you!!! for usefull post

  24. chris says:

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

    Merry Xmas

  25. Tidza says:

    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 says:

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

Leave a Reply