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

10,549

Solution 1

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.

Solution 2

Add an f:selectItem before the f:selectItems, with an empty value, or something like "please select ...", and mark it as non-selectable.

Solution 3

Just preload the desired data in the (post)constructor of the bean.

if (codigoTipoAutorizacion != null) {
    // Preload all other desired data as well.
}
Share:
10,549
Pablo
Author by

Pablo

Updated on June 27, 2022

Comments

  • Pablo
    Pablo about 2 years

    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.