views:

24

answers:

1

Hi everyone! (ps: sorry about my english)

I have a text field in my application and for this textfield I have a a4j:support that should work on onchange event! So I have the same form for Include and Update. The user fill the textfield with a code and in my bean I have a method that search the equivalent combobox if that value exists then set it to my object of my bean, this works fine for include but not for update! I don't know what this could be. I will post the code.

My xhtml page

<ui:decorate template="../templates/edit.xhtml">
                    <ui:param name="idComponente" value="codTipoVinculo" />
                    <ui:define name="label">Tipo Vínculo: </ui:define>
                    <h:inputText id="codTipoVinculo"
                        value="#{cadastroPrestadorBean.codTipoVinculo}"
                        size="4" maxlength="1"
                        styleClass="#{validationUtil.invalid('codTipoVinculo', facesContext) ? 'invalido' : ''}"
                        onkeypress="return(soEntraNumero(event,this));" 
                        onmouseout="soArrastaNumero(this);" 
                        onblur="soArrastaNumero(this);">
                        <a4j:support event="onchange" 
                                     action="#{cadastroPrestadorBean.findByKey(cadastroPrestadorBean.prestador.tipoVinculo)}"
                                     ajaxSingle="true"
                                     immediate="true"
                                     focus="codTipoPgtoMatmed"
                                     reRender="nomeTipoVinculo, codTipoVinculo, outputMessagesInForm">
                            <a4j:actionparam noEscape="true"
                                value="(document.getElementById('formPrestador:codTipoVinculo').value == '' ? '-11111' : document.getElementById('formPrestador:codTipoVinculo').value)"
                                assignTo="#{cadastroPrestadorBean.codTipoVinculo}" />                                                        
                        </a4j:support>
                    </h:inputText>
                    <rich:comboBox enableManualInput="false"  defaultLabel="Selecione uma opção" id="nomeTipoVinculo"
                        value="#{cadastroPrestadorBean.prestador.tipoVinculo}"
                        converter="simpleIndexConverterTipoVinculo">
                        <f:selectItems
                            value="#{cadastroPrestadorBean.listaTipoVinculo }" />
                        <a4j:support event="onchange" reRender="codTipoVinculo, outputMessagesInForm"
                            ajaxSingle="true" limitToList="true"
                            action="#{cadastroPrestadorBean.findByKey(cadastroPrestadorBean.prestador.tipoVinculo)}" />
                    </rich:comboBox>
                </ui:decorate>

My bean method

if (object instanceof TipoVinculo) {
            if ( codTipoVinculo == null || codTipoVinculo == -11111) {
                prestador.setTipoVinculo(new TipoVinculo());
                return;
            }
            for (SelectItem element : listItemsTipoVinculo) {
                if ( ( ((TipoVinculo)element.getValue()).getCodTipoVinculo().intValue() ) == codTipoVinculo ){
                    prestador.setTipoVinculo((TipoVinculo)BeanUtils.cloneBean(element.getValue()));
                    achou = true;
                }
            }
            if ( !achou ){
                prestador.setTipoVinculo(new TipoVinculo());
                addMessageInfo("Tipo Vínculo inválido.");
            }
            achou = false;
        }

This is the method that populate the listItemsTipoVinculo

    public List<SelectItem> getListaTipoVinculo() {
    try {
        if ( listItemsTipoVinculo.size() == 0 ){
            List<TipoVinculo> list = tipoVinculoBusiness.listaTiposVinculos();
            for (TipoVinculo item : list) {
                listItemsTipoVinculo.add(new SelectItem(item));
            }
        }
    } catch (CommonBusinessException e) {
        addMessageError(e);
    }
    return listItemsTipoVinculo;
}

The combobox is previously populated in the listItemsTipoVinculo so when the page start I have it populated. When the user put the code in the textfield it should call the findbykey method but they don't ! just in my update screen when my object 'prestador' is populated.

A: 

Well, I found out what was happening... THe issue was in my form for Inclusion I was setting all my objects to new instances and for update just ones who has values in database...

So I just check in the bean if the field came null then create a new instance!

if ( prestador.getTipoVinculo()==null ){
    prestador.setTipoVinculo(new TipoVinculo());
}

solve my problem... Any way, thanks to all who read this!

Jorge Campos