ravinikam
ravinikam

Reputation: 3696

Submitting <h:selectManyListBox> causes "Validation Error: Value is not valid"

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.

Upvotes: 1

Views: 7511

Answers (2)

anonymous
anonymous

Reputation: 31

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

<converter>
    <converter-for-class>...
    <converter-class>...
</converter>

In the h:selectManyListbox I had no converter specified.

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

<converter-id>myConv</converter-id>

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

Upvotes: 3

Adeel Ansari
Adeel Ansari

Reputation: 39887

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.

Upvotes: 2

Related Questions