7

I have a selectMenu, with list of (SelectItems) defined in handler as follows

Handler { List(SelectItem) stateList; State state; }
State { String stateCd; }

JSF Code::

<h:selectOneMenu value="#{state.stateCode}">
  <f:selectItems value="#{handler.stateList}">
</h:selectOneMenu>

Now my list is in requestScope, and I see submitted value is string and is present in the list but I still get "Validation error: Value is not valid".Can someone assist.

Community
  • 1
  • 1
ccp_123
  • 87
  • 1
  • 1
  • 5

1 Answers1

32

Validation Error: Value is not valid

This means that the selected item does not match any of the items available in the list. I.e., the stateCode.equals(stateList.get(i)) has never returned true for any of the items.

This can happen when the stateList is empty during validations phase, or when the equals() method of the value type is not (properly) implemented.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC for the reply, I am loading the list init() onPreRender(), and the list is in request scope, I am getting this error on Save,could it be the reason for this error, If can you suggest how can I fix this apart from maintining in session scope thanks. – ccp_123 Dec 30 '10 at 22:26
  • Either the loading logic is bogus, or the `equals()` is not (properly) implemented. You're the only one who can find that out. Just debug the form submit. – BalusC Dec 30 '10 at 22:29
  • I see that in the Validate method the list is empty, but on page load the list is created and is in request scope, so why will the list be empty? It doesn't make sense to me to maintain it in session scope to fix this issue. – ccp_123 Dec 30 '10 at 22:46
  • 2
    You need to prepare the list during bean's construction or `@PostConstruct`. I have no idea what that `onPreRender()` thing is doing, that is not standard JSF, but it sounds like as if it is doing the job during the render response phase, it's too late then. – BalusC Dec 31 '10 at 00:57