Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 321
Pablo Avatar asked Dec 19 '25 11:12

Pablo


2 Answers

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.

like image 79
Christophe Roussy Avatar answered Dec 22 '25 01:12

Christophe Roussy


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

if (codigoTipoAutorizacion != null) {
    // Preload all other desired data as well.
}
like image 20
BalusC Avatar answered Dec 22 '25 02:12

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!