1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ravinikam
  • 3,666
  • 6
  • 28
  • 25
  • 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. – ravinikam Nov 20 '09 at 07:39
  • Are you using Spring Faces? If yes, have a look here, jira.springframework.org/browse/FACES-4 – Adeel Ansari Nov 20 '09 at 07:50
  • no we are not using spring faces. – ravinikam Nov 20 '09 at 12:31

2 Answers2

3

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.

agf
  • 171,228
  • 44
  • 289
  • 238
anonymous
  • 31
  • 1
  • Specifically, collections can only use Strings. See "Core JavaServer Faces" by Geary & Horstmann 3rd edition, p. 163. Roughly: "The value of a selectMany tag can be a collection instead of an array, but ... the elements cannot be converted because the collection's element type is not known at runtime. ... That means, you should use collections only for strings." – Pixel Dec 01 '11 at 22:10
2

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
  • 39,541
  • 12
  • 93
  • 133
  • 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. – ravinikam Nov 20 '09 at 07:38
  • Try putting some debug messages in your equals method to see exactly whats going on. – Adeel Ansari Nov 20 '09 at 07:43
  • Are you using Spring Faces? If yes, have a look here, http://jira.springframework.org/browse/FACES-4 – Adeel Ansari Nov 20 '09 at 07:49
  • I believe that there are multiple causes of the Value is not valid message, and this was probably not the OP's problem. See the comment by @anonymous. – Pixel Dec 01 '11 at 22:12