Search code examples
jsf-2richfaces

h:selectOneMenu default item to null, clear empty item value, required field


Hello I'm having trouble with the following piece of code:

<h:selectOneMenu id="selectTipoAutorizacion"
                                                    value="#{autorizacion.codigoTipoAutorizacion}"
                                                    required="true">
                                                    <f:selectItems
                                                        value="#{cc.attrs.controller.getListaTiposAutorizacion(autorizacion)}"
                                                        var="tipoAutorizacion"
                                                        itemLabel="#{tipoAutorizacion.nombreTipoAutorizacion}"
                                                        itemValue="#{tipoAutorizacion.id.codigoTipoAutorizacion}" />

                                                    <a4j:ajax event="change" execute="@this"
         listener = #{myListener.listener}                                                                                      render="selectAutorizador" />
                                                </h:selectOneMenu>

The problem is that the default selected value is always the first one of the tag. And that's bothering the users, cause some data is loaded based on the selected item value..., however that info it's not loaded until the change event occurs (a4j:ajax tag), so right now the user has to select another item, and then select the previous one in order to see the default's item related info.

I addressed the problem by loading the default's item related info at the beginning, however the user doesn't like this. Because it could lead to confusion. So the question is... how could I avoid that behaviour? What I want is the selectOneMenu to load with a clear value( Like if there weren't any f:selectItems). Thanks a lot.


Solution

  • Your field is required, there should be nothing valid to default to in this case. Add an empty selectItem to the top of the list: selectItems.add(0, new SelectItem("", "")); or this way: <f:selectItem itemValue="" itemLabel="" /> By default it would then select the empty selectItem. The user will be forced to make a choice as the required="true" does not allow an empty selection.