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.
Hey Nuwan,
I guess you just saved me some hours of googling 😉
Thanks mate!
Lorenz
LikeLike
where i write this methode , boolean equal(). ??
LikeLike
Am glad to hear mate..
cheers !!
LikeLike
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
LikeLike
is this in the equals method, or in the converter ?
LikeLike
You just saved me hours of head scratching! Cheers!
LikeLike
It’s great!
Thank’s, you save me:D:D!
LikeLike
Thanks a lot for your suggestion. Adding the equals method fixed the issue 🙂
LikeLike
Thank you very much. Saved my life!
LikeLike
thanks a lot. Now I can sleep well to night!
LikeLike
Thanks very much for that. I’m amazed that this isn’t in any of the reference / tutorial material I have read.
LikeLike
Can some one please explain whose overide method is this equals(Object obj) method.
LikeLike
yes it is the Objects’ equals method, since all classes are inheriting that.
cheers!
LikeLike
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.
LikeLike
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
LikeLike
Hi Nuwan, your post has been of great help for me. Thanks in advance.
Elkin
LikeLike
Excelent!!!!!, this tip can save my life, thanks.
LikeLike
Hi,
where we have to write the code for equals method.
Kamlesh
LikeLike
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
LikeLike
Thank you very much… saved my days 🙂
LikeLike
thanks a lot my friend, iwas about to be crazy by this problem
LikeLike
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.
LikeLike
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 🙂
LikeLike
Thank you!!! for usefull post
LikeLike
Hi saved me hours too. Done it before and forgot about it. Thanks to Google and thanks to you !
Merry Xmas
LikeLike
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.
LikeLike
Thanks a lot. Couldn’t understand why a regular Converter wasn’t working. Got caught by a cut and paste in the Hibernate class.
LikeLike
Thank you! I can sleep at nights after 2 days of nightmare. Now I wonder why I didn’t google this before.
LikeLike
Excellent..worked like a charm for me
LikeLike
Hay….Rally Nice ………Excellent……
Thankyou very much
LikeLike
hi
think you for thid helpful postwhere can i declare the equals methode ???do you have a full example
LikeLike
brillant !!!!!
thank you for this.
LikeLike
I want to kiss you.
LikeLike
Thank you so much!!!!
LikeLike
Finally working 🙂
LikeLike
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.
LikeLike