0

i have a dialog with two input fields and a commandButton, to add an object. If the user leaves a blank field a message growl is shown and the dialog stays open. But if the user completes all the fields and press the commandButton the object is added but the dialog doesn't close.

View.xhtml

<h:form id="allIncidenciasAux">
    <div id="divBotonCrear">
        <p:commandButton value="Añadir Incidencia" type="button" onclick="PF('dlg1').show();"/>
    </div> <br/>

    <p:tabView>
        <p:tab title="All">
            <ui:repeat value="#{incidenciaBean.allIncidencias}" var="aux" >
                <p:panel id="basic" header="#{aux.titulo}" style="margin-bottom:20px" toggleable="true" collapsed="true" styleClass="#{ aux.solucionado eq 'false' ? 'incNS' : null}">
                    <h:panelGrid columns="2">
                        <h:panelGrid style="background-color: #B6C2DE; padding: 10px;">
                            <h:panelGrid columns="2">
                                <h:outputText value="Fecha de publicacion: " style="font-weight: bold"/>
                                <h:outputText value="#{aux.fecha_publicacion}"/>
                            </h:panelGrid>
                            <h:panelGrid columns="2">
                                <h:outputText value="Autor: " style="font-weight: bold"/>
                                <h:outputText value="#{aux.nombreCompleto}"/>
                            </h:panelGrid>
                            <h:panelGrid columns="2">
                                <h:outputText value="Solucionado: " style="font-weight: bold"/>
                                <h:outputText value="#{aux.solucionado ? 'Si' : 'No'}"/>
                            </h:panelGrid>
                        </h:panelGrid>

                        <h:panelGrid>
                            <h:outputText value="Descripción: " style="font-weight: bold"/>
                            <h:outputText value="#{aux.descripcion}"/>
                        </h:panelGrid>
                    </h:panelGrid> <br/>
                </p:panel>
            </ui:repeat>
        </p:tab>
    </p:tabView>
    <p:growl id="growl" showDetail="true" sticky="false" life="8000"/>
</h:form> 

<h:form id="dialogAddIncidencia">
    <p:dialog id="newIncDialogo" header="Nueva Incidencia" widgetVar="dlg1" minHeight="40" resizable="false">
        <p:ajax event="close" listener="#{incidenciaBean.handleClose}" update=":dialogAddIncidencia"/> 
        <p:outputLabel value="Título"/> <br/>
        <p:inputTextarea id="tituloEdit" value="#{incidenciaBean.titulo}" rows="2" cols="50" counter="displayTA" maxlength="50" counterTemplate="{0} caracteres restantes" autoResize="true" required="true" requiredMessage="Título necesario"/> <br/>
        <p:outputLabel id="displayTA"/> <br/> <br/>

        <p:outputLabel value="Descripción: "/> <br/>
        <p:inputTextarea id="descripcion" value="#{incidenciaBean.descripcion}" rows="10" cols="50" counter="displayDA" maxlength="200" counterTemplate="{0} caracteres restantes" autoResize="true" required="true" requiredMessage="Descripción necesaria"/> <br/>
        <p:outputLabel id="displayDA"/> <br/> <br/>

        <p:commandButton value="Guardar Incidencia" action="#{incidenciaBean.insertIncidencia()}" oncomplete="if (#{incidenciaBean.camposNoVacios()}) dlg1.hide()" update=":allIncidenciasAux  :allIncidenciasAux:growl"/>
        <p:messages for="titulo"/> 
        <p:messages for="descripcion"/> 
    </p:dialog>    
</h:form>

Methods of incidenciaBean.java

public void insertIncidencia(){        
    HttpSession httpSession = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    UsuarioBean usuarioHttp = (UsuarioBean) httpSession.getAttribute("usuarioSession");
    this.idComunidad = usuarioHttp.idComunidad;
    this.idUsuario = usuarioHttp.getIdUsuario();
    this.solucionado = false;
    this.fecha_solucion = null;
    DateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY");
    Date date = new Date();
    this.fecha_publicacion = dateFormat.format(date);

    if(ejb_incidencia.existIncidenciaTitulo(this.titulo, this.descripcion)){
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error:",
                "Ya existe una incidencia con el mismo título y descripción."
              + "Por favor, compruebe que no se trata de la misma incidencia."));
    } else{
        ejb_incidencia.realizaRegistroIncidencia(this);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info:",
                "La incidencia se ha registrado correctamente"));
        initialStatusIncidencia();
    }

}

public boolean camposNoVacios(){
    return !(this.titulo == null || this.descripcion == null);
}

public void initialStatusIncidencia(){
    this.titulo = null;
    this.descripcion = null;
}

public void handleClose (CloseEvent event){
    initialStatusIncidencia();
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Megan..
  • 25
  • 6
  • Next time please start creating an [mcve]. Remove more and more code until it starts to work. I bet that if you change `oncomplete="if (#{incidenciaBean.camposNoVacios()}) dlg1.hide()"` to `oncomplete="dlg1.hide()"` it works.. The 'EL' in there is only evaluated when the html is rendered. Not when the oncomplete needs to be called. And next time also always post version info. See [ask], and http://www.stackoverflow.com/tags/jsf/info to – Kukeltje Jun 14 '16 at 19:18

1 Answers1

0

You are correct in using oncomplete; however, I don't think your if (#{incidenciaBean.camposNoVacios()}) statement ever evaluates true. The dialouge only hides if camposNoVacios() returns ture(if bothtitulo and descripcion aren't null). In insertIncidencia()'s else statement (which I assume is the success case) you set them to null with initialStatusIncidencia().

So your program sets titulo and descripcion, checks that they aren't null in insertIncidencia(), and then sets them to null before checking to see if they aren't null with camposNoVacios().

ezPaint
  • 152
  • 1
  • 8