“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.

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

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

    Like

  2. 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.

    Like

  3. 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

    Like

  4. 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

    Like

  5. 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.

    Like

  6. 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 🙂

    Like

  7. 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.

    Like

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

    Like

  9. Hey, guys I know this comes a century later; I’d like to add just a useful link for anyone who needs more detail on this.
    Check BalusC explanation
    http://stackoverflow.com/questions/9069379/validation-error-value-is-not-valid

    I particularly was frustrated even after tryin Nuwand’s advice because I was using String values yet still got the error.

    My problem was using a RequestScoped bean and performing business logic in the getter method.

    Like

Leave a comment