3

I have h:selectOneMenu component in my page. User has to select "Yes" or "No" there, initial value is null (labelled "Unknown"). In bean it's Boolean, so null is possible. If user selects valid value, e.g. "Yes", hits Next, true is saved in the bean.

Now user can go back to this page, and select "Unknown", which is not a valid value. When user hits Next now, error message is shown. It is ok, but value is changed to "Yes" then. It is updated from the bean. This way it changes what user entered, which is not required behaviour. Is it possible to keep user's value?

<h:form id="co">
  <h:selectOneMenu id="isCeded"
                   value="#{bean.boolValue}"
                   required="true"
                   requiredMessage="#{msgs.please_select_value}">
    <f:selectItem itemLabel="#{msgs.unknown}"/>
    <f:selectItem itemValue="true" itemLabel="#{msgs.yes}"/>
    <f:selectItem itemValue="false" itemLabel="#{msgs.no}"/>
    <f:ajax execute="@this" render="isCededMsg"/>
  </h:selectOneMenu>*
  <h:message id="isCededMsg" for="isCeded" errorClass="invalid"/>

  <h:commandButton id="submit" value="Next" action="#{bean.submit}" />
</h:form>

What is strange, this behaviour takes place only when user enters null to fields where null is not valid value. If e.g. user enters another invalid value in some validated text field (e.g. 20 digits where only 10 are allowed) invalid value entered by user remains after clicking "Next". Only error message apperas.

amorfis
  • 15,390
  • 15
  • 77
  • 125

1 Answers1

0

This appears to be a bug, that doesn't look like getting fixed. See the JIRA link mentioned in this question: h:selectOneMenu with Please select displays previous selection on validation error

The question and the JIRA issue have the solution. You need to extend the MenuRenderer and override the renderOption method with the patched code mentioned at the end of the JIRA issue.

Community
  • 1
  • 1
annihilate
  • 598
  • 1
  • 5
  • 16