JSF Updating forms - forms

I am trying to submit some data in JSF. The problem is that I have two forms and the button which I want to trigger the update is outside of the forms, here I place my code so that you can understand better what I mean. Because of that I cannot submit any data to the server.
<ui:define name="mainArea">
<h:form id="form">
<div class="row">
<div class="col-md-5">
<p:outputLabel for="rfqName" value="Name" />
<br></br>
<p:inputText id="rfqName" required="true" style="width:100%;"
value="#{data.data.rfqName}" maxlength="100">
<p:ajax event="change" update="#form"/>
</p:inputText>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p:outputLabel for="rfqVersion" value="Version" />
<br></br>
<p:inputText id="rfqVersion" required="true" style="width:60%;"
value="#{data.data.rfqVersion}" maxlength="20"></p:inputText>
</div>
</div>
</h:form>
</ui:define>
<ui:define name="actionSection">
<p:commandButton actionListener="#{logic.close}" value="Proceed" update=":form"
styleClass="pull-right">
</p:commandButton>
<p:commandButton value="Save" styleClass="pull-right" />
</ui:define>

You can use the primefaces RemoteCommand. This allows you to click the button on the form from the outside.
<ui:define name="mainArea">
<h:form id="form">
<div class="row">
<div class="col-md-5">
<p:outputLabel for="rfqName" value="Name" />
<br></br>
<p:inputText id="rfqName" required="true" style="width:100%;"
value="#{data.data.rfqName}" maxlength="100">
<p:ajax event="change" update="#form" />
</p:inputText>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p:outputLabel for="rfqVersion" value="Version" />
<br></br>
<p:inputText id="rfqVersion" required="true" style="width:60%;"
value="#{data.data.rfqVersion}" maxlength="20"></p:inputText>
</div>
</div>
<p:remoteCommand name="remoteClose" actionListener="#{logic.close}"
update=":form" styleClass="pull-right" />
<p:remoteCommand name="remoteSave" value="Save"
styleClass="pull-right" />
</h:form>
</ui:define>
<ui:define name="actionSection">
<p:commandButton type="button" onclick="remoteClose()" value="Proceed" />
<p:commandButton type="button" onclick="remoteSave()" value="Save"
styleClass="pull-right" />
</ui:define>

Related

Contact form position

I'm trying to get the 'contact' form on my page in the middle of the webpage.
<form id="contact_form" action="#" method="POST" enctype="multipart/form-data">
<div class="row">
<label for="name">Uw naam:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="email">Uw e-mail:</label><br />
<input id="email" class="input" name="email" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="message">Uw vraag:</label><br />
<textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
</div>
<input id="submit_button" type="submit" value="Verstuur bericht" />
</ul>
</form>
use css:
#contact_form {width:400px;display:block;margin:0 auto;}
contact_form {position:absolute; top:50px;left:10px;width:500px;display:block;margin:0 auto;}

JSF Form with a null value

I got a issue with my view, that I can't solve.
My view has 6 forms but when I do the main submit a entire form has null values and a inputText in another form has null too, that's my view:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
<h:head>
<title>Novo Cliente</title>
</h:head>
<h:body>
<ui:composition template="templateAdmin.xhtml">
<ui:define name="conteudoAdmin">
<!-- inicio conteudo de novo cliente -->
<div class="row mt">
<div class="col-lg-12">
<div class="form-panel">
<h4 class="mb"><i class="fa fa-angle-right"></i> Novo Cliente</h4>
<h:form class="form-horizontal style-form" id="tipoCliente">
<th></th>
<h:commandButton class="btn btn-primary" value="Pessoa Física" action="#{clienteController.pessoaFisica()}" />
<p:spacer width="75" height="10" />
<h:commandButton class="btn btn-success" value="Pessoa Jurídica" action="#{clienteController.pessoaJuridica()}" />
<br></br>
<br></br>
<br></br>
</h:form>
<h:form class="form-horizontal style-form" id="buscaCliente" >
<!--inicio condicao CPF CNPJ-->
<div class="form-group">
<h:outputLabel class="col-sm-2 col-sm-2 control-label" value="CPF" for="cpf" rendered="#{clienteController.cliente.tipoPessoa == 0}" />
<h:outputLabel class="col-sm-2 col-sm-2 control-label" value="CNPJ" for="cnpj" rendered="#{clienteController.cliente.tipoPessoa == 1}" />
<div class="col-sm-10">
<p:inputMask mask="999.999.999-99" id="cpf"
value="#{clienteController.cliente.identificacao}"
rendered="#{clienteController.cliente.tipoPessoa == 0}"
/>
<p:inputMask mask=" 99.999.999/9999-99" id="cnpj"
value="#{clienteController.cliente.identificacao}"
rendered="#{clienteController.cliente.tipoPessoa == 1}"
/>
<p:spacer width="55" height="10" />
<h:commandButton id="verifcarCliente" value="Verificar Cliente"
actionListener="#{clienteController.buscaClienteCPFCNPJ()}" >
</h:commandButton>
</div>
</div>
</h:form>
<h:form class="form-horizontal style-form" id="dadosCliente" rendered="#{clienteController.existeCliente == false}" >
<div class="form-group">
<h:outputLabel value="Razão Social" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{clienteController.cliente.nomeFantasia}" class="form-control" />
</div>
</div>
<div class="form-group">
<h:outputLabel value="Nome" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{clienteController.cliente.nome}" class="form-control" />
</div>
</div>
<div class="form-group">
<h:outputLabel for="mask" value="Data de Nascimento" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<p:inputMask mask="99/99/9999" id="dataNasc"
value="#{clienteController.dataNascimento}" />
</div>
</div>
<div class="form-group">
<h:outputLabel class="col-sm-2 col-sm-2 control-label" value="Telefone" for="tel" />
<div class="col-sm-10">
<p:inputMask mask="(99)9999-9999" id="tel"
value="#{clienteController.cliente.telefone}"/>
</div>
</div>
<div class="form-group">
<h:outputLabel value="E-mail" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText id="email" value="#{clienteController.cliente.email}" class="form-control" required="true"
pt:placeholder="ex: email#dominio.com"
validatorMessage="Endereço de email inválido.">
<f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]#[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<h:message for="email" />
</div>
</div>
<div class="form-group">
<h:outputLabel value="Site" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText id="site" value="#{clienteController.cliente.site}" class="form-control"
validatorMessage="Endereço de site inválido.">
<f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_].[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<h:message for="site" />
</div>
</div>
</h:form>
<h:form class="form-horizontal style-form" id="novoClienteCEP" rendered="#{clienteController.existeCliente == false}">
<div class="form-group">
<h:outputLabel class="col-sm-2 col-sm-2 control-label" value="CEP" for="cep" />
<div class="col-sm-10">
<h:inputText id="cep" value="#{clienteController.cep}"
pt:placeholder="Somente Números" maxlength="8" required="true"
validatorMessage="Digite somente os Números">
<f:validateRegex pattern="[0-9]+" />
<f:validateLength minimum="8" maximum="8" />
</h:inputText>
<p:spacer width="55" height="10" />
<h:commandButton id="Buscar_Dados" value="Buscar Dados"
actionListener="#{clienteController.buscarDadosCEP}" >
<f:ajax execute="#form" render="novoClienteCEPDados"/>
</h:commandButton>
</div>
</div>
</h:form>
<!--inicio autopreenchimento de endereço-->
<h:form class="form-horizontal style-form" id="novoClienteCEPDados" rendered="#{clienteController.existeCliente == false}">
<ui:repeat value="#{clienteController.enderecos}" var="dadosEndereco" >
<h:column >
<div class="form-group">
<h:outputLabel value="Logradouro" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{dadosEndereco.tipoEndereco} #{dadosEndereco.logradouro}"
class="form-control" disabled="true" />
</div>
</div>
<div class="form-group">
<h:outputLabel value="Número" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{clienteController.numero}" class="form-control" disabled="false"
pt:placeholder="Somente Números" required="true"
validatorMessage="Digite somente os Números">
<f:validateLength minimum="1" />
</h:inputText>
</div>
</div>
<div class="form-group">
<h:outputLabel value="Bairro" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{dadosEndereco.bairro}" class="form-control" disabled="true" />
</div>
</div>
<div class="form-group">
<h:outputLabel value="Cidade" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{dadosEndereco.cidade}" class="form-control" disabled="true" />
</div>
</div>
<div class="form-group">
<h:outputLabel value="Estado" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{dadosEndereco.UF}" class="form-control" disabled="true" />
</div>
</div>
</h:column>
</ui:repeat>
</h:form>
<h:form class="form-horizontal style-form" id="novoClienteSalvar" rendered="#{clienteController.existeCliente == false}">
<!--fim auto preenchimeto endereco-->
<h:commandButton id="Salvar" value="Salvar" action="#{clienteController.salvar}" >
<f:ajax execute="#all" render="#all"/>
</h:commandButton>
<p:spacer width="95" height="10" />
<h:commandButton value="Cancelar" type="reset"/> <p:spacer width="25" height="10" />
</h:form>
</div>
</div>
</div>
<!-- fim conteudo de novo cliente -->
</ui:define>
</ui:composition>
</h:body>
The form dadosCliente and this inputText is null
<div class="form-group">
<h:outputLabel value="Número" class="col-sm-2 col-sm-2 control-label" />
<div class="col-sm-10">
<h:inputText value="#{clienteController.numero}" class="form-control" disabled="false"
pt:placeholder="Somente Números" required="true"
validatorMessage="Digite somente os Números">
<f:validateLength minimum="1" />
</h:inputText>
</div>
</div>
All other forms are correct, if I nest some forms It works, but I read that's not a good thing to do.
Anyone??

JSF <f:validator> and Bootstrap do not work together

I am trying to get my JSF Register form working with Bootstrap styles. Everything works fine, but i do not know how i can integrate my validation messages into my form. If i render this code the validation messages are just ignored. I don't know why? Any idea how i can make them visible and maybe style them good-looking? Many thanks in advance for your help!
<!-- Register Form -->
<h:form role="form" style="width: 400px; margin: 0 auto;">
<!-- User name -->
<div class="required-field-block">
<h:outputLabel for="name" value="Benutzername"></h:outputLabel>
<h:inputText id="name" value="#{userBean.user.name}"
validatorMessage="gültigen Benutzernamen mit 4-116 Zeichen" class="form-control">
<f:validateLength minimum="4" maximum="16" for="name"></f:validateLength>
</h:inputText>
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<!-- Password -->
<div class="required-field-block">
<h:outputLabel for="password" value="Passwort"></h:outputLabel>
<h:inputSecret id="password" value="#{userBean.user.password}"
class="form-control" required="true"
requiredMessage="Mailadresse angeben">
</h:inputSecret>
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<!-- Email -->
<div class="required-field-block">
<h:outputLabel for="email" value="Email"></h:outputLabel>
<h:inputText id="email" value="#{userBean.user.email}"
class="form-control" required="true"
requiredMessage="Passwort angeben">
<!-- Email Validation -->
<f:validator validatorId="emailValidator"></f:validator>
</h:inputText>
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<!-- Age -->
<div>
<h:outputLabel for="age" value="Alter"></h:outputLabel>
<h:inputText id="age" value="#{userBean.user.age}" class="form-control"></h:inputText>
<h:message for="age"></h:message>
</div>
<h:commandButton id="send" value="Register" type="submit"
action="#{userBean.addUser}" class="btn btn-primary"></h:commandButton>
<h:message for="send"></h:message>
<h:commandButton id="reset" value="Reset" type="submit"
action="#{userBean.reset}" immediate="true" class="btn"></h:commandButton>
<h:message for="send" errorClass="errorMessage" globalAll="true"></h:message>
</h:form>
Just use jsf tag <h:message/> under each required input with attribute for where you should point input's id.
Also for good-looking style you can add bootstrap class "text text-danger". Also you can use attribute label for <h:inputText>.
It means you will get following error:
Email:Validation Error
instead of
email-id: Validation error.
For example:
<h:outputLabel for="email" value="Email"></h:outputLabel>
<h:inputText id="email-id" label="Email" value="#{userBean.user.email}"
class="form-control" required="true"
requiredMessage="Passwort angeben">
<!-- Email Validation -->
<f:validator validatorId="emailValidator"></f:validator>
</h:inputText>
<h:message for="email-id"/>

I have two forms on a page but the submit button is submitting both forms

Hi I have two forms on a page, one is a small newsletter sign up form and the other is a larger event booking form. when the large booking form is submitted it submits the small newsletter form aswell. think it has something to do with the action url.
Here is the page code:
<script type="text/javascript"><!--
function validate(f){
var regex = /^\s*$/i;
for(var i=0; i<f.elements.length; i++){
if(regex.test(f.elements[i].value)){
alert("Please fill in all fields.");
f.elements[i].focus();
return false;
}
}
if(f.user_email.value.indexOf('#',0)==-1 || f.user_email.value.indexOf('.',0)==-1)
{
alert("You must provide a VALID email address.");
f.user_email.focus();
return false;
}
return true;
}
//--></script>
<div id="eventform" />
<form action="/Booking?ename=testevent&edate=19%20October%202011&submitform=yes" method="post" onsubmit='return validate(this);'>
<fieldset class="fieldset">
<div class="leftform">
<label for="booking_name">Event: </label><br class="nobr" />
<input name="booking_name" type="text" id="booking_name" value="testevent" />
</div>
<div class="rightform">
<label for="event_date">Date: </label><br class="nobr" />
<input name="event_date" type="text" id="event_date" value="19 October 2011" />
</div>
<div class="clear"></div>
<div class="leftform">
<label for="user_name">Name: </label><br class="nobr" />
<input name="user_name" type="text" id="user_name" />
</div>
<div class="rightform">
<label for="organisation">Organisation: </label><br class="nobr" />
<input name="organisation" type="text" id="organisation" />
</div>
<div class="clear"></div>
<div class="leftform">
<label for="address">Address: </label><br class="nobr" />
<input name="address" type="text" id="address" />
</div>
<div class="rightform">
<label for="postcode">Postcode: </label><br class="nobr" />
<input name="postcode" type="text" id="postcode" />
</div>
<div class="clear"></div>
<div class="leftform">
<label for="user_telephone">Contact Number: </label><br class="nobr" />
<input name="user_telephone" type="text" id="user_telephone" />
</div>
<div class="rightform">
<label for="user_email">Email Contact: </label><br class="nobr" />
<input name="user_email" type="text" id="user_email" />
</div>
<div class="clear"></div>
<br />
<hr />
<h3>Attendees</h3>
<p>Please list the name(s) and email address(s) of those you wish to book a place at the above event.</p>
<div class="leftform">
<input placeholder="Name" name="attendee1" type="text" id="attendee1" />
</div>
<div class="rightform">
<input placeholder="Email Address" name="attendee_email1" type="text" id="attendee_email1" />
</div>
<div class="clear"></div>
<div class="leftform">
<input placeholder="Name" name="attendee2" type="text" id="attendee2" />
</div>
<div class="rightform">
<input placeholder="Email Address" name="attendee_email2" type="text" id="attendee_email2" />
</div>
<div class="clear"></div>
<div class="leftform">
<input placeholder="Name" name="attendee3" type="text" id="attendee3" />
</div>
<div class="rightform">
<input placeholder="Email Address" name="attendee_email3" type="text" id="attendee_email3" />
</div>
<div class="clear"></div>
<div class="leftform">
<input placeholder="Name" name="attendee4" type="text" id="attendee4" />
</div>
<div class="rightform">
<input placeholder="Email Address" name="attendee_email4" type="text" id="attendee_email4" />
</div>
<div class="clear"></div>
<div class="leftform">
<input placeholder="Name" name="attendee5" type="text" id="attendee5" />
</div>
<div class="rightform">
<input placeholder="Email Address" name="attendee_email5" type="text" id="attendee_email5" />
</div>
<div class="clear"></div>
<br />
<hr />
<h3>Invoice Details</h3>
<p>Please give details of where the invoice should be sent.</p>
<label for="invoice_name">Name: </label><br class="nobr" />
<input name="invoice_name" type="text" id="invoice_name" /><br />
<label for="invoice_address">Address: </label><br class="nobr" />
<input name="invoice_address" type="text" id="invoice_address" /><br />
<label for="invoice_postcode">Postcode: </label><br class="nobr" />
<input name="invoice_postcode" type="text" id="invoice_postcode" /><br />
<p>Once we have received your booking form the person booking and those attending will receive a confirmation email confirming your places at the event and an invoice will be issued.
If you have any questions please do not hesitate to contact.</p>
</fieldset>
<br />
<input id="bookingform_submit" class="submitform" type="submit" value="Submit" />
<br /><br />
</form>
</div>
</div>
</div>
<div class="clear"></div>
</div></div>
<!--/content-->
<!--footer-->
<div id="outer-footer">
<div id="footer">
<div class="footer-1">
<h6>Get in touch...</h6>
<ul>
<li>Suite 124-128 Baltic Chambers,50 Wellington Street Glasgow G2 6HJ.</li>
<li><span>Tel:</span> 0141 248 1242</li>
<li><span>Fax:</span> 0141 221 1911</li>
<li><span>Email Us:</span>info#tis.org.uk </li>
</ul>
</div>
<div class="footer-2">
<h6>Join our newsletter...</h6>
<ul>
<li>Hear about the latest event and courses.</li>
<script type="text/javascript"><!--
function validate(f){
var regex = /^\s*$/i;
for(var i=0; i<f.elements.length; i++){
if(regex.test(f.elements[i].value)){
alert("Please fill in all fields.");
f.elements[i].focus();
return false;
}
}
if(f.user_email.value.indexOf('#',0)==-1 || f.user_email.value.indexOf('.',0)==-1)
{
alert("You must provide a VALID email address.");
f.user_email.focus();
return false;
}
return true;
}
//--></script>
<li>
<form action="./&submitform=yes" method="post">
<span class="input_space">
<input name="user_name" id="user_name" type="text" align="left" onblur="if(this.value=='')this.value='Your Name';"
onfocus="if(this.value=='Your Name')this.value='';" value="Your Name" />
</span>
<span>
<input name="user_email" id="user_email" type="text" align="left" onblur="if(this.value=='')this.value='Your Email Address';"
onfocus="if(this.value=='Your Email Address')this.value='';" value="Your Email Address" />
</span>
<input id="newsletterform_submit" type="submit" value="" class="submit-2" />
</form>
I dont think it is submitting the form twice, i think that the variable "submitform" = yes is being set by both, so when you click through to the large form it thinks that form 2 has been submitted also - but in reality it hasn't... you probably want to check that the form has really been submitted using the $_POST variables.

How to define form action in JSF?

In order to replace the default Spring security login form I came up with this solution:
<form name="f" action="../j_spring_security_check" method="POST" >
<h:panelGrid columns="2">
<h:outputText value="Username" />
<h:inputText id="j_username" />
<h:outputText value="Password" />
<h:inputText id="j_password" />
</h:panelGrid>
<h:commandButton value="Login" />
</form>
But instead of plain <form> tag I would like to use <h:form> since Primefaces components only work within <h:form>. Using <h:form> the form action will be set automatically by JSF to the current page, not to the value set by me in the example above. Where to I have to code the action "../j_spring_security_check" now? I tried putting it into the <h:commandButton> as follows but that doesn't work:
<h:form name="f">
<h:panelGrid columns="2">
<h:outputText value="Username" />
<h:inputText id="j_username" />
<h:outputText value="Password" />
<h:inputText id="j_password" />
</h:panelGrid>
<h:commandButton value="Click here" action="../j_spring_security_check" />
</form>
It leads to the error message Unable to find matching navigation case with from-view-id '/login.xhtml' for action '../j_spring_security_check' with outcome '../j_spring_security_check'.
Is it the only way to define a navigation case in faces-config.xml? I want to avoid using a bean for this simple use case.
The best solution for me is to use the default <button> tag instead. Since typical button styling is not being applied (in my case I am using Primefaces), I set it manually. Here's the whole result:
<h:outputStylesheet library="primefaces" name="jquery/ui/jquery-ui.css" />
<h:outputStylesheet library="css" name="login.css" />
<div class="message">
<c:if test="#{param.error == 1 and SPRING_SECURITY_LAST_EXCEPTION != null}">
<span class="error">#{SPRING_SECURITY_LAST_EXCEPTION.message}</span>
</c:if>
</div>
<div class="login">
<form action="../j_spring_security_check" method="post">
<h:panelGrid columns="2">
<h:outputText value="Username" />
<h:inputText id="j_username" />
<h:outputText value="Password" />
<h:inputSecret id="j_password" />
</h:panelGrid>
<div class="submit">
<button type="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
<span class="ui-button-text">Login</span>
</button>
</div>
</form>
</div>
<h:form id="login" prependId="false" onsubmit="document.getElementById('login').action='j_security_check';">
Your action attribute should be an EL expression like follows:
action="#{managedBean.method}"
Your best chance is to use the onclick property of the button. This is an example with a primefaces commandButton, but it will also work for jsf components because it uses the (native) javascript onclick function.
<h:form>
<p:commandButton value="Submit" onclick="alert('JS!')" actionListener="#{tweetBean.addTweet()}" update=":tabView,:trends,:tweetsnr" />
</h:form>