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"/>
Related
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>
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??
i have some probelm in spring tag form input..
if i use <form:input>..there will be error displayed
java.io.IOException: JspException when evaluating the body but if i used html tag, it's can't connect to database.
<form:form method="POST" action="/sampling/insert"
modelAttribute="sampling">
<div class="form-row control-group row-fluid">
<label class="control-label span3" name="populasi" for="normal-field">Population</label>
<div class="controls span9">
<form:input path="populasi" type="text" id="normal-field"
class="row-fluid" />
</div>
</div>
<div class="form-row control-group row-fluid">
<label class="control-label span3" for="normal-field">Error
Rate</label>
<div class="controls span9">
<form:input path="tKesalahan" name="tKesalahan" type="text"
id="normal-field" class="row-fluid" />
</div>
</div>
<input type="submit" class="btn btn-success" rel="tooltip"
data-placement="top" value="Submit" />
</form:form>
Try this:
name, type are not valid attributes
change the tag attributes:
<form:input path="tKesalahan" id="normal-field" cssClass="row-fluid" />
Reference: spring input tag attributes
I am looking for a way to pre-populate a Spring MVC form from values stored in a session-scoped bean. (using this namespace: http://www.springframework.org/tags/form).
For example, say I have added a queryInfo object to the uiModel.
How do I display the name instance variable from the queryInfo object?
<form:input path="queryInfo.name" />
Is this possible? If so how?
In your request mapping, add the bean to the model:
model.addAttribute("queryInfo", queryInfo);
Then use modelAttribute in the form tag to bind it to the form:
<form:form id="some-form" modelAttribute="queryInfo">
...
Now name will display (provided there is a getter in your object appropriately named) when you do this:
<form:input path="name" />
Keep in mind form:input is a child tag of form:form. It is not meant to be used on its own.
#skel625's solution for form:input is perfect, but in the case of the form:select, with different option how do I set attributes? because in this way only works for the form:input
My form:select is like this:
<form:select path="dolorefastidio">
<option value="1"><spring:message code="questionnaire.compile.label.paindiscomfort.one"/></option>
<option value="2"><spring:message code="questionnaire.compile.label.paindiscomfort.two"/></option>
<option value="3"><spring:message code="questionnaire.compile.label.paindiscomfort.three"/></option>
<option value="4"><spring:message code="questionnaire.compile.label.paindiscomfort.four"/></option>
<option value="5"><spring:message code="questionnaire.compile.label.paindiscomfort.five"/></option>
</form:select>
the solution is put in a value attribute the bean attribute like this
Exam Register Form
<div class="form-group has-success">
<label for="code" class="col-lg-3 control-label">Exam Code</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="cod" placeholder="code" value="${editExam.cod}"/>
<form:errors path="cod" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="name" class="col-lg-3 control-label">Exam Name</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="name" placeholder="name" value="${editExam.name}"/>
<form:errors path="name" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="teachNme" class="col-lg-3 control-label">Teacher Name</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="teachName"
placeholder="Teacher Name" value="${exams.teachName}" />
<form:errors path="teachName" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="vote" class="col-lg-3 control-label">Final Grade</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="vote"
placeholder="Vote" value="${exams.vote}" />
<form:errors path="vote" cssClass="error" />
</div>
</div>
<div class="form-group has-success">
<label for="cfu" class="col-lg-3 control-label">Credits (CFU)</label>
<div class="col-lg-9">
<form:input type="text" class="form-control" path="cfu"
placeholder="Credits" value="${editExam.cfu}" />
<form:errors path="cfu" cssClass="error" />
</div>
</div>
<input type="submit" value="Save"
class="btn btn-primary pull-right">
</fieldset>
</form:form>
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.