Make the outline of a input "red" if field is empty - primefaces

I use primefaces 4, template : cupertino.
I want to make the outline of a input "red" if field is empty.
I upgraded to type field required = "true", but the outline remain in their original state if they are empty.
But for fields of type HTML 5 all goes well.
Thank you.
<form id="form_inscri_pp" name="form_inscri_pp">
<h:panelGrid columns="2" cellpadding="5" >
<h:outputText value="Nom:" styleClass="label_inscri_pp"/>
<input id="nom_pp" class='inscription_candidat'
placeholder='Saisissez votre nom' type='text' name="nom_pp"
pattern="^[A-Za-z ]+$" maxlength="25" required="required"
autocomplete="on"/>
<h:outputText value="Date de naissance: " styleClass="label_inscri_pp"/>
<p:calendar name="DN_pp" id="DN_pp" value="#{calendrier.date}"
locale="fr" showOn="button" navigator="true"
pattern="dd/MM/yyyy" mindate="1/1/1950" maxdate="31/12/1993"
mask="true" required="true" readonly="true" size="20" />
</h:panelGrid>
<input class='button_inscri' type='submit' value="S'inscrire" />
<input class='button_inscri' type='button' value='RĂ©initialiser' />
</form>

According the code you post, the input text with id nom_pp get red-outlined when I input nothing. The only thing that won't work is p:calendar, which is because you put a readonly attribute in it.
So the solution is simply remove the readonly attribute of the p:calendar.
And BTW, there is no name attribute in p:calendar, so remove it, too.
What's more, you need to use <h:form> instead of <form>. And there is also no name parameter in <h:form>.
So you code should looks like:
<h:form id="form_inscri_pp" >
<h:panelGrid columns="2" cellpadding="5" >
<h:outputText value="Nom:" styleClass="label_inscri_pp"/>
<input id="nom_pp" class='inscription_candidat'
placeholder='Saisissez votre nom' type='text' name="nom_pp"
pattern="^[A-Za-z ]+$" maxlength="25" required="required"
autocomplete="on"/>
<h:outputText value="Date de naissance: " styleClass="label_inscri_pp"/>
<p:calendar id="DN_pp" value="#{inputValidateBean.date}"
locale="fr" showOn="button" navigator="true"
pattern="dd/MM/yyyy" mindate="1/1/1950" maxdate="31/12/1993"
mask="true" required="true" size="20" />
</h:panelGrid>
<input class='button_inscri' type='submit' value="S'inscrire" />
<input class='button_inscri' type='button' value='RĂ©initialiser' />
</h:form>
UPDATE
Since the solution above doesn't work in your environment, try to replace all HTML5 components to primeface/jsf components.
I tried that and it also works in my environment.
Take a look at this:
<h:form id="form_inscri_pp" >
<h:panelGrid columns="2" cellpadding="5" >
<h:outputText value="Nom:" styleClass="label_inscri_pp"/>
<p:inputText id="nom_pp" styleClass="inscription_candidat"
placeholder="Saisissez votre nom" maxlength="25"
required="true" autocomplete="on" value="#{inputValidateBean.nom}">
<f:validateRegex pattern="^[a-zA-Z]+$" />
</p:inputText>
<h:outputText value="Date de naissance: " styleClass="label_inscri_pp"/>
<p:calendar id="DN_pp" value="#{inputValidateBean.date}"
locale="fr" showOn="button" navigator="true"
pattern="dd/MM/yyyy" mindate="1/1/1950" maxdate="31/12/1993"
mask="true" required="true" size="20" />
</h:panelGrid>
<p:commandButton styleClass="button_inscri" value="S'inscrire"
validateClient="true" update="form_inscri_pp"
action="#{inputValidateBean.doSubmit()}"/>
</h:form>
In the backing bean:
#ManagedBean
#ViewScoped
public class InputValidateBean implements Serializable{
private Date date;
private String nom;
public void doSubmit(){
System.out.println("Do something!");
}
//getters and setters below...

Related

Primefaces DataTable Selection's Attribute is Null if not rendered in Dialog

I'm not sure if this is a limitation or a coding error on my part. I have a Primefaces DataTable that allows the user to select it (single select radio) and then click on a CommandButton to edit the data in a Dialog.
In the HTML code where I display the selected object, I omitted the ID, Lat, and Long attributes because a human user shouldn't be touching these attributes. The Dialog is showing the selected attributes correctly, however, when I tried to find the existing record using the selected object's ID for update, it thrown a no result error. When I checked the selected object's ID attribute, it has a value of NULL.
I tinker around with the HTML code and found that in order to have the any of the attributes copied to the selected object, the attributes must be shown in the XHTML. When I include the attribute as with Render = False, the attribute will have a value of NULL. Any suggestions on hiding attributes that human users shouldn't modify beside using Disable tag attribute?
Class Property{
Int ID;
String Address;
Long Lat;
Long Long;
String Desc;
}
public void modifyCurrentProject() {
HashMap<String, Object> params = new HashMap();
params.put("id", currentProject.getId());
//fails to find Project by ID as currentProject.getID == null
Project temp = (Project) dbu.findByNamedQuery("project.findID", params);
//Omit statements to overwrite attached object's values before writing back to database
dbu.modifyEntity(); //dbu is another object to control database access
currentProject = null;
}
<h:body>
<p:growl id="projectMsg" showDetail="true"/>
<h:form id="form">
<p:dataTable id="projectTable" var="project" value="#{projectBacking.builder.projects}" selection="#{projectBacking.currentProject}" rowKey="#{project.id}">
<f:facet name="header">My Project List
<p:commandButton value="Add" type="button" onclick="PF('projectAdd').show()"/>
<p:commandButton value="Edit" update=":editForm" oncomplete="PF('projectEdit').show()"/>
</f:facet>
<p:column selectionMode="single"/>
<p:column headerText="ID">
<h:outputText value="#{project.id}"/>
<p:column headerText="Description">
<h:outputText value="#{project.property.description}"/>
</p:column>
<p:column headerText="Street">
<h:outputText value="#{project.street}"/>
</p:column>
<p:column headerText="City">
<h:outputText value="#{project.city}"/>
</p:column>
<p:column headerText="Province">
<h:outputText value="#{project.state}"/>
</p:column>
<p:column headerText="Latitude">
<h:outputText value="#{project.latitude}"/>
</p:column>
<p:column headerText="Longitude">
<h:outputText value="#{project.longitude}"/>
</p:column>
<p:column headerText="Status">
<h:outputText value="#{project.status}"/>
</p:column>
</p:dataTable>
</h:form>
<!-- Omitted the Add Project Dialog box -->
<p:dialog header="Edit Project" modal="true" height="400" width="80%" widgetVar="projectEdit">
<h:form id="editForm">
<p:panel id="editProject" header="Edit Project" toggleable="true" closable="false">
<!--<h:inputText id="id" value="#{projectBacking.currentProject.id}"/>-->
<h:inputText id="country" value="#{projectBacking.currentProject.country}"/>
<h:inputText id="lat" rendered="true" value="#{projectBacking.currentProject.latitude}"/>
<h:inputText id="long" rendered="true" value="#{projectBacking.currentProject.longitude}"/>
<h:outputLabel for="desc" value="Description" />
<h:inputText id="desc" value="#{projectBacking.currentProject.description}"/>
<h:outputLabel for="street" value="Street" />
<h:inputText id="street" value="#{projectBacking.currentProject.street}"/>
<h:message for="street"/>
<h:outputLabel for="city" value="City:" />
<h:inputText id="city" required="false" value="#{projectBacking.currentProject.city}"/>
<h:message for="city" />
<h:outputLabel for="state" value="Province" />
<h:inputText id="state" required="true" value="#{projectBacking.currentProject.state}"/>
<h:commandButton value="Save Project" type="submit" action="#{projectBacking.modifyCurrentProject()}">
</h:commandButton>
</p:panel>
</h:form>
</p:dialog>
</h:body>
After placing some more breakpoints in the code, I discovered that upon saving the changes in the Dialog, it's creating a new backing bean. If the is set to Render=false, Disabled=true, or ReadOnly=true then new backing bean's object's value will not be set and retain the initial value as specified in the object's constructor.
Basically, the cause is incorrect scope. I was using Request scope when the problem occurred. However, changing to Session scope resolved it.

JSF Validation message not displaying in template's content part

I got problem with non displaying validation error message on my form, when I'm using templates.
It does validation ok, as I can see I am not forwarded to next page, but I dont see error message.
When I try same code without defining layout template parts it prints message.
This is my form code:
<h:form>
<h:panelGrid columns="2">
<h:outputLabel for="mname">Username </h:outputLabel>
<h:inputText required="true" requiredMessage="Username is required.">
</h:inputText>
<h:commandButton value="Submit"></h:commandButton>
</h:panelGrid>
</h:form>
And this is my code when I'm defining content part:
<!-- Content -->
<ui:define name="content">
<h:form>
<h:panelGrid columns="2">
<h:outputLabel for="mname">Username </h:outputLabel>
<h:inputText required="true" requiredMessage="Username is required.">
</h:inputText>
<h:commandButton value="Submit"></h:commandButton>
</h:panelGrid>
</h:form>
</ui:define>
Anyone got reason why wouldn't I see error message when I'm defining my layout page.
This is my layout.xhtml content part.
<div id="content">
<ui:insert name="content">
<ui:include src="/template/content.xhtml" />
</ui:insert>
</div>
Eventually you will need a h:message tag.. the requiredMessage is a tip for the framework on what to print in the message tag.
So you should have a set up like:
<h:form>
<h:message showSummary="true" showDetail="false"
id="errorsMessages"
for="txt"/>
<h:panelGrid columns="2">
<h:outputLabel for="mname">Username </h:outputLabel>
<h:inputText id="txt" required="true" requiredMessage="Username is required.">
</h:inputText>
<h:commandButton value="Submit"></h:commandButton>
</h:panelGrid>
</h:form>

Control on Calendar Primefaces component

I am using p:calendar Primefaces component for specifying 2 dates : start and end.
An evident control is to verify if end date is not before start date.
Is there a way to do it ?
I found the solution :
<p:outputLabel for="startDate" value="#{msg.StartDate}" />
<p:calendar id="startDate" value="#{employeeBean.employee.startDate}" required="true" pattern="dd-MM-yyyy" language="#{msg.CalendarLanguage}" mask="true"
requiredMessage="#{msg.required}">
<p:ajax event="dateSelect" update="endDate" />
</p:calendar>
<p:message for="startDate" />
<p:outputLabel for="endDate" value="#{msg.EndDate}" />
<p:calendar id="endDate" mindate="#{employeeBean.employee.startDate}" value="#{employeeBean.employee.endDate}" required="false" pattern="dd-MM-yyyy"
language="#{msg.CalendarLanguage}" mask="true">
</p:calendar>
<p:message for="endDate" />

Read only text box size increased size automatically

I have the two text box(Using JSJ).
I have given txt box size as 50. It is working fine. Based on logic I have make one of the text to readonly disabled = true or disable = disabled. The read only text width is increasing its size.
When I inspect through f12,the size are showing 50 only,In inspect mode when i remove the
disable = disabled,it is reducing to actual size
I checked by the sample.
<h:inputText value="test" type="text" size="50">
</h:inputText>
<br/><br/>
<h:inputText value="tes1t" type="text" size="50" disabled="true">
</h:inputText>
I have pass the param to form as below :
Param
<ui:param name="validationMessageOnTheSameLine" value="true" />
<ui:param name="rowWidth" value="650px" />
<ui:param name="helpTextPaddingLeft" value="130px" />
<ui:param name="showAsterik" value="false" />
<ui:param name="showAsterikForUsername" value="false" />
<ui:param name="inputTextSize" value="50" />
<h:panelGrid columns="2" style="width: #{rowWidth}" id="emailValidated"
columnClasses="#{columnClass1}, #{columnClass2}">
<h:outputLabel value="#{uit.emailAddress}:" for="email">
<h:outputLabel styleClass="asterik" value="*"
rendered="#{showAsterik}" />
</h:outputLabel>
<h:panelGrid columns="2" styleClass="tableNoPadding">
<h:inputText value="#{userBean.emailAddress}" id="email"
styleClass="ux-margin-right-1t" size="#{inputTextSize}"
required="true" disabled="#{disableEmailInput}"
requiredMessage="#{uitkem.valueRequired}"
validatorMessage="#{uitkem.emailValidator}">
<f:validator validatorId="emailValidator" />
</h:inputText>
<rich:message for="email" />
</h:panelGrid>
</h:panelGrid>
Check that these <h:inputText> are enclosed in <h:form>.
Also, you don't need to add type="text" in <h:inputText>.
From my test environment (JSF 2.2), both these inputs have the same size when rendered.

PrimeFaces Tabview Tab change not working

I have simple flow of 3 tabs. I have tested the Simple tabview example on showcase.
But When I change the first tab with my content, I can't switch to other tabs.
Kindly guide me with what is wrong that needs to be changed.
Earlier I was using Wizard but I had the same tab change problem there as clicking on next was taking me to the last tab everytime.
and Now facing other issue with Tabview
I am posting the code:
<h:form id="compositionmaster">
<p:tabView id="tabView">
<p:tab id="tab1" title="Committee Details">
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Committee Type: " />
<p:selectOneMenu id="type" value="#{userWizard.comm.committeeType}" effect="fade">
<f:selectItem itemLabel="----Select----" itemValue="0" />
<f:selectItem itemLabel="New" itemValue="1" />
<f:selectItem itemLabel="Existing" itemValue="2" />
</p:selectOneMenu>
<h:outputText value="Concerned Division: " />
<p:selectOneMenu id="division" value="#{userWizard.comm.committeeSubType}" effect="fade">
<f:selectItem itemLabel="----Select----" itemValue="0" />
<f:selectItem itemLabel="Administration" itemValue="1"/>
<f:selectItem itemLabel="Finance" itemValue="2" />
<f:selectItem itemLabel="Marketing" itemValue="3" />
<f:selectItem itemLabel="Others" itemValue="4" />
</p:selectOneMenu>
<h:outputText value="Committee Name: " />
<p:inputText value="#{userWizard.comm.committeeName}" maxlength="100"/>
<h:outputText value="Subject: " />
<p:inputText value="#{userWizard.comm.committeeSubject}" maxlength="100" />
<h:outputText value="Description: " />
<p:inputText value="#{userWizard.comm.committeeDescription}" maxlength="500" />
<h:outputText value="Tenure of Committee: " />
<p:panelGrid columns="2" >
<p:inplace id="Tenure" label="From">
<p:calendar value="#{userWizard.comm.startDate}" id="start" showOn="button" />
</p:inplace>
<p:inplace label="To">
<p:calendar value="#{userWizard.comm.endDate}" id="end" showOn="button" />
</p:inplace>
</p:panelGrid>
<h:outputText value="Add Document: " />
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{fileBean.handleFileUpload}" mode="advanced" update="messages" multiple="true" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png|doc|pdf)$/"/>
</h:form>
</h:panelGrid>
</p:tab>
<p:tab id="tab2" title="Godfather Part II">
<h:panelGrid columns="2" cellpadding="10">
<h:outputText id="tab2Text" value="stature grows."/>
</h:panelGrid>
</p:tab>
<p:tab id="tab3" title="Godfather Part III">
<h:panelGrid columns="2" cellpadding="10">
<h:outputText id="tab3Text" value="After a promise that his family would one day be completely legitimate."/>
</h:panelGrid>
</p:tab>
</p:tabView>
</h:form>
Don't use nested forms
HTML doesn't allow nested forms, so you shouldn't use them in primefaces either.They can cause undesired behavior.
See: other stackoverflow questions, or a nice collection of things to avoid in JSF: here (note point 2)
This is the only problem I see in your code. Other than that in rare cases you might have problems if using ajax and not processing the whole form. But as I can see this is not the case in your code.