Pablo
Pablo

Reputation: 3467

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.

Upvotes: 3

Views: 10738

Answers (3)

Christophe Roussy
Christophe Roussy

Reputation: 16999

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.

Upvotes: 8

user207421
user207421

Reputation: 310859

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

Upvotes: 2

BalusC
BalusC

Reputation: 1108642

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

if (codigoTipoAutorizacion != null) {
    // Preload all other desired data as well.
}

Upvotes: 2

Related Questions