1

I have a JSF form and a managed bean. Upon submission of my form one of the inputs returns

sendMessage:template: Validation Error: Value is not valid

I believe these are the names of the form and input. However I have no validation at all on the field either in the JSF page or on the bean. How is this possible?

here is the field in question:

<p:selectOneMenu id="template" value="#{sendMessageController.template}">  
                    <f:selectItems value="#{sendMessageController.availableTemplates}"  />
                </p:selectOneMenu>  

and the managed bean:

@ManagedBean(name="sendMessageController")
@RequestScoped
public class SendMessageController {
    ....
    private String template;
    private List<String> availableTemplates = new ArrayList<String>();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mark W
  • 5,824
  • 15
  • 59
  • 97

1 Answers1

2

This validation error is recognizable from UISelectOne/UISelectMany components where <f:selectItem(s)> are being used, such as <h:selectOneMenu>. This validation error will occur when the selected value has not returned true on the equals() check with any of the available items.

This has in turn three possible causes:

  1. The equals() method of the select item value is missing or broken.
  2. The list of available items has incompatibly changed between displaying and submitting the form.
  3. If any involved, the Converter has returned the wrong item or even null in getAsObject().

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I think my problem is 2. I have two other inputs which are used to calculate what is available in the selectOne – Mark W Nov 21 '13 at 09:59
  • 1
    Then just make sure that it doesn't incompatibly change. E.g. put the bean in view scope, or recreate the same list in (post)constructor of the request scoped bean. If some additional parameters are required for this, just pass them along. – BalusC Nov 21 '13 at 10:00
  • Debugging it appears the problem is that after submit the postconstruct is called, and at that point the values for the other fields used to get availableTemplates are null (even though they have been populated in the page by the user) so Im unable to reset availableTemplates. I do not understand why this is, as I'd already made a call to a web service to get those values, I'd prefer to avoid doing it again. – Mark W Nov 21 '13 at 10:17
  • 1
    It's because your bean is **request** scoped. Every HTTP request creates logically a new bean instance. Displaying the form counts as one HTTP request. Each ajax request counts as one HTTP request. Submitting the form counts as one HTTP request. That's just how HTTP works. Again, as advised, put bean in view scope to keep the instance alive as long as you're interacting with the same form. See also http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/ – BalusC Nov 21 '13 at 10:18
  • doh. Of course. It was supposed to be session scoped. – Mark W Nov 21 '13 at 10:20