I'm working on a huge JSF project and for this question I've shortened the most important part so I can be understood. Thing is, the user must fill a registration form, which in this case requests its identification (identificacion) and to click on "Completar registro" button to call a javascript function that requests the registration confirmation for sending the form through the commandButton and show a "cargando" ($cargando) modal. Once the request reaches the server, the user is registered and it executes the r_registrar javascript function which will hide the "cargando" ($cargando) modal.
My problem is that if the user doesn't write its identification (identificacion), the function registrar() from bean won't be called and hence. it won't execute the javascript function r_registrar, therefore, the "cargando" modal doesn't hide.
I wish I could identify if there's any mistake in the sent form. In case there's a mistake, I want the "cargando" modal to hide.
Thank you very much.
Código XHTML:
<h:form id="formRegistrar">
<h:panelGroup id = "pg_Registrar">
<h:outputLabel class="output" id="outputIdentificacion" for="inputIdentificacion" value="Identificación"/>
<b:inputText class="input" id="inputIdentificacion" type="text" required="true" value="#{beanUsuario.usuarioSesion.identificacion}"/>
<div class="alert alert-danger"><h:message for="inputIdentificacion"/></div>
<b:commandButton id = "btnRegistrar" binding="#{beanUsuario.btnRegistrar}" style="display:none" class="btnRegistrar" value="" action="#{beanUsuario.registrar}">
<f:ajax execute="pg_Registrar" render="pg_Registrar"/>
</b:commandButton>
<b:button value="Completar registro" class="btn btn-primary" onclick="registrar(); return false;"/>
</h:panelGroup>
</h:form>
Código Bean (java):
public void registrar(){
try{
bd_insertarUsuario(this.usuarioSesion);
}catch(Exception e){
//...
}finally{
RequestContext.getCurrentInstance().execute("r_registrar()");
}
}
Código JavaScript:
var $cargando = $('<div class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-hidden="true" style="padding-top:15%; overflow-y:visible;"><div class="modal-dialog modal-m"><div class="modal-content"><div class="modal-header"><h3 style="margin:0;">Cargando</h3></div><div class="modal-body"><img width = "100%" class = "center-block" src = "../resources/imgs/loading.gif"/></div></div></div></div></div>');
function mostrarCargando() {
$cargando.modal();
}
function ocultarCargando() {
$cargando.modal('hide');
}
function registrar(){
var c = confirm("seguro?");
if(c){
$(".btnRegistrar").click();
setTimeout(mostrarCargando,100);
}
}
function r_registrar(){
ocultarCargando();
}