tags:

views:

1720

answers:

3

Pals, I got stuck again while my web development, I am providing multiple selection list to user for selecting many option. A Fragment of JSF Page

<h:selectManyListbox id="associatedAS" value="#{maintainForm.selectedAS}"> <s:selectItems value="#{maintainForm.associatedAS}" var="as" label="#{as.name}" /> <rmc:asConverter /> </h:selectManyListbox>

but the problem is that when submit the page I am getting error on console

sourceId=maintainForm:associatedAS[severity=(ERROR 2), summary=(maintainForm:associatedAS: Validation Error: Value is not valid), detail=(maintainForm:associatedAS: Validation Error: Value is not valid)]

I am not able to figure out why this is happening, the item I am displaying in list is not string so I have written converter 'asConverter' for converting values from other objects to string and vice-versa. Also the Value I given above in tag ' #{maintainForm.selectedAS} ' is of type List (selectedAS).

Any kind of help appreciated.

Thank you.

+2  A: 

This problem occurs when you send some values to the page, and then some or all of the original values sent got modified, or some new values got added on the client. As you already know, that JSF keep its view state on the server or client, depends how you configured it, so it validates the component using that state on submit. In your case it found out that the values sent to the client are no more the same. Hence you end up getting this error.

If you are using a custom converter, as I describe on the converters page, you have to provide a working equals method for the object that you are trying to convert to and from. If you attempt to use the default equals method or fluff the implementation the object won't convert correctly leading to the rather non-intuitive error message: "Validation Error: Value is not valid". - ref: crazysquirrel.com

Another similar suggestion.

Adeel Ansari
thanks for you time Vinegar.
RN
Actually I have already implemented equals method and custom converter and it is working fine with selectOneMenu but I am facing this for selectManyListBox. So I dont think converter would be a problem.
RN
Try putting some debug messages in your equals method to see exactly whats going on.
Adeel Ansari
Are you using Spring Faces? If yes, have a look here, http://jira.springframework.org/browse/FACES-4
Adeel Ansari
A: 

To the point, this validation error will arise whenever the selected item(s) does not match any of the items of the SelectItem list during the form submit.

This can have two causes:

  1. The selected item is not part of the SelectItem list anymore.
  2. The Object#equals() incorrectly returned false for the selected item.

To fix 1), ensure that the getter of the <f:selectItems> returns exactly the same list during the form submit as it was during the page display. An easy fix is to place the bean in the session scope, but this has too much impact on other areas. Just ensure that the same list is loaded during bean's construction/initialization.

To fix 2) ensure that the Object#equals() and Object#hashcode() are implemented as per the contract in the java.lang.Object API. Here's a basic example:

public class Bean {
    private Long id; // Use whatever property which denotes the uniqueness of the data.
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public boolean equals(Object other) {
        return (other instanceof Bean) && (id != null) && id.equals(((Bean) other).id);
    }

    public int hashCode() {
        return (id != null) ? (getClass().hashCode() + id.hashCode()) : super.hashCode();
    }
}
BalusC
+1  A: 

I had the same problem as Ravi Nikam. Equals method and converter are implemented, and it works fine with a selectOneMenu, but it gives a nice "Validation Error: Value is not valid" with a selectManyListBox. After searching for some hours, I found a solution. The selectManyListbox ist based on javax.faces.component.UISelectMany. The javadoc of the UISelectMany says:

Obtain the Converter using the following algorithm: If the component has an attached Converter, use it. If not, look for a ValueExpression for value (if any). The ValueExpression must point to something that is: * An array of primitives (such as int[]). Look up the registered by-class Converter for this primitive type. * An array of objects (such as Integer[] or String[]). Look up the registered by-class Converter for the underlying element type. * A java.util.Collection. Do not convert the values.

So the last point in this list caused my problem: "Do not convert the values".

I had specified in faces-config.xml

... ...

In the h:selectManyListbox I had no converter specified.

I solved the problem by adding to faces-config.xml

myConv

and by adding the attribute converter="myConv" to the h:selectManyListbox tag.

anonymous
Thank you very much :)
volothamp