dev@javaserverfaces.java.net

SelectOneMenu not showing null value selected if required=true

From: Michael Youngstrom <youngm_at_gmail.com>
Date: Tue, 1 May 2007 10:29:14 -0600

Here is the basic situation. If I have a selectOneMenu with
required="true" and I select a null option after previously selecting
a not null option then when the selectOneMenu is rendered the
perviously selected option is selected instead of the null option.

Example:
I've got a selectOneMenu:

<h:selectOneMenu id="menu" value="#{someBean.someValue}" required="true">
     <f:selectItem itemLabel="Select An Item"/>
     <f:selectItems value="#{someBean.someOptions}" />
</h:selectOneMenu>

<h:inputText id="text" required="true"/>

If I select a value in "menu" and submit this form and get a
validation error because I didn't enter anything in "text" and "menu"
will correctly have selected the item I attempted to select on the
previous form submission. However, if I now select the item labeled
"Select An Item" in "menu" and submit the form again, instead of
"Select An Item" selected it shows the item I had previously selected.

This functionality differs from that of an inputText with
required=true. If I give inputText a value and submit the form with a
validation error and then clear out the inputText and submit the form
with a validation error again then an empty inputText is rendered not
the previously successful submitted value.

Looking through the code of MenuRenderer the culprit appears to be
renderOption() calling the method containsaValue(). containsaValue()
determines that a null value was submitted and causes the renderer to
defer to the components getValue() for a value to select.

Reading through the SelectMany render docs on rendering option
elements there is a note that if the submittedValue is null to obtain
a value from getValue() but I'm not sure if this condition was put
into the render docs for this specific situation or for some other
situation that this situation is a side effect.

I think that it is strange for the user if they select a valid option
in a SelectOneMenu and then latter attempt to select a "null" option
then the user will see a "required" validation error next to the
selectOneMenu they tried to select "null" on but it will have their
previously successful value selected instead of the null value. Seems
like undesired functionality to me.

Any thought?

Mike