I have two field dates on my Jsf form.
Date 1 : DOB and Date 2: Date_Retire
When the user will select a date from Date_retire, it should be greater than 18 years old.
<h:outputText value="DOB:" />
<p:calendar pattern="mm/dd/yyyy" id="dob" value="#{ActorComponent.actor.dob}"
required="false" converter="primefacesCalendarConverter" />
<h:outputText value="Date retired:" />
<p:calendar pattern="mm/dd/yyyy" id="dateretired"
value="#{ActorComponent.actor.dateretired}"
required="false" converter="primefacesCalendarConverter" >
<p:ajax event="dateSelect" listener="#{ActorComponent.minimumDate()}"
process= "dateretired"" >
</calendar>
Listener:
public void minimumDate(Calendar Object){
Calendar dob=actor.getdob();
dob.add(Calendar.Year,18);
if(Object.before(dob)){
FacesContext.getCurrentInstance().addMessage(
"FacesMessage.SEVERITY_ERROR",
new FacesMessage("Date is not valid."));
}
}
The ajax is not working. Facescontext is not displaying the message. Anyone can help?
I'm not sure if "dateSelect" is a real event, try using "click" or "onclick"
<p:ajax event="click"
Related
I have a form with multiple fields, of which I need one of them required to continue or a group of them. For simplicity, I'll just paste a simpler version of my code here
<h:form id="searchForm">
<p:growl for="searchForm" showDetail="true" showSummary="true"/>
<p:inputText id="name"
value="#{backingBean.name}"
required="#{backingBean.nameRequired and backingBean.masterRequired}">
<p:ajax />
</p:inputText>
<h:panelGroup id="dateForm">
<p:inputMask id="date"
value="#{backingBean.date}"
mask="99/99/9999"
required="#{backingBean.dateRequired and backingBean.masterRequired}"
converterMessage="Please insert using format dd/mm/yyyy">
<f:convertDateTime pattern="dd/MM/yyyy" />
<f:validator validatorId="dateValidator" />
<p:ajax update="dateForm" />
</p:inputMask>
</h:panelGroup>
<p:selectOneMenu id="documentType" effect="fade"
value="#{backingBean.documentType}"
required="#{backingBean.documentRequired and backingBean.masterRequired}">
<f:selectItem itemLabel="Select document type" noSelectOption="true" />
<f:selectItems
value="#{constantsController.docType.entrySet()}"
var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
<p:ajax />
</p:selectOneMenu>
<p:inputText id="documentNumber"
value="#{backingBean.documentNumber}"
required="#{backingBean.documentRequired and backingBean.masterRequired}">
<p:ajax />
</p:inputText>
<p:commandButton id="searchButton"
value="Search"
action="#{controllerBean.submitSearch}"
icon="ui-icon-search">
<p:ajax
listener="#{backingBean.checkRequired()}" update="#form" />
</p:commandButton>
</h:form>
When all fields are empty, the is shown as it should. Now what I need is the inputMask date to only show the converter message and the validation message. I need it to not show the message when the requiredMessage will try to be shown.
I tried putting a <p:message for="date" rendered="#{!empty param['date']}" /> but it does not work, since it is not rendered on the first load, and ajax does not update the panelGroup, I think it is because since I gave an invalid value to the converter, it does not fire the update.
Additional information:
As you can see, I have multiple required fields, I need at least one of these:
-name
-date
-document type + document number
Now, to validate the required fields, I'm doing the following on my backing bean:
public void checkRequired() {
nameRequired = name.equals("");
dateRequired = date == null;
documentRequired = documentType.equals("") || documentNumber.equals("");
masterRequired = nameRequired && dateRequired && documentRequired;
if(masterRequired)
{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Incomplete form",
"Please insert at least 1 field");
FacesContext.getCurrentInstance().addMessage("searchForm", message);
return;
}
}
I've got a form with two primefaces calendar input elements and a datatable on it. The calendar items are used to filter the result displayed in the table as from to values. Therefore I use an p:ajax ‘dateSelect’ event which is working fine when doing the manipulation with the mouse over the popup. But when I change the date manually in the input field with the keybord no change event is fired. When I use a second p:ajax event for blur or change I'm not able to get the new value.
<p:calendar id="startDate"
value="#{filterStart}"
required="true"
showOn="button"
maxdate="#{filterEnd}"
>
<p:ajax event="dateSelect"
listener="#{listController.onFilterStartChanged}"
update="filterTbl, endDate" />
<p:ajax event="change"
listener="#{listController.onFilterStartBlured}"
update="filterTbl, endDate"
process="#this"
partialSubmit="true" immediate="true" />
</p:calendar>
the method onFilterStartChanged gets called when selection is made over the pupup it's possible to read the new value:
public void onFilterStartChanged(final SelectEvent event) {...}
but the Keyboard changes are not fired when moving Focus to another component
public void onFilterStartBlured(final AjaxBehaviorEvent event) {
Date newDate = (Date)((Calendar)event.getSource()).getValue();
...
}
how can I get the new date?
I have same need with p:calendar in Primefaces 5.3 and it's work for me whit this code. I don't use event parameter
<p:calendar id="startDate"
value="#{filterStart}"
required="true"
showOn="button"
maxdate="#{filterEnd}">
<p:ajax event="dateSelect"
listener="#{listController.onFilterStartChanged()}"
update="filterTbl, endDate" />
<p:ajax event="change"
listener="#{listController.onFilterStartChanged()}"
update="filterTbl, endDate" />
</p:calendar>
no parameter in the method, value is in filterStart
public void onFilterStartChanged() {...}
I am having some problems and couldn't figure them out after 3 days of search. I am using JSF + PrimeFaces 3.5 + Ajax and it is not working correctly.
Basically I have a startDate and numberOfDays attributes. I want to calculate endDate automatically using ajax when both startDate and numberOfDays are informed.
These are the problems:
It is not updating the attribute endDate. The listener is called (I've breakpointed it!) and the attribute is calculated for sure.
The listener is called just once. I can't make it run twice!
This is my xhtml page:
<div>
<h:outputLabel for="startDate">
<h:outputFormat value="startDate" />
<h:inputText id="startDate" value="#{bean.startDate}">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
</h:inputText>
</h:outputLabel>
</div>
<div>
<h:outputLabel for="numberOfDays">
<h:outputFormat value="numberOfDays" />
<h:inputText id="numberOfDays" value="#{bean.numberOfDays}">
<p:ajax listener="#{bean.calculateEndDate}" process="startDate,numberOfDays" update="panelGroupEndDate" partialSubmit="true" />
</h:inputText>
</h:outputLabel>
</div>
<div>
<h:panelGroup id="panelGroupEndDate">
<h:outputLabel for="endDate">
<h:outputFormat value="endDate" />
<h:inputText id="endDate" value="#{bean.endDate}">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
</h:inputText>
</h:outputLabel>
</h:panelGroup>
</div>
Here is my Managed Bean
public void calculateEndDate()
{
if(startDate != null && numberOfDays != null)
{
endDate = Util.calculateEndDate(startDate, numberOfDays);
}
else
{
endDate = null;
}
}
I have ajax codes running in different pages without a problem but all these codes are triggered by a commandButton. These same example works if I use a commandButton to trigger the listener.
Does anyone know what I am doing wrong? Thanx a lot! :)
Check this out:
<p:ajax listener="#{bean.calculateEndDate}" process="startDate,numberOfDays, #this" update="panelGroupEndDate" />
I have faced the problem that the listener method is not called, sometimes adding #this to the end of process can make it right.
i have 2 autofill fileds empname and empno if i give empno remaining fields should be autofilled from database,same to empname.but the problem is When i insert new empno remaining entered fields has been set to null,same to empname how to solve it.
<h:outputText value="Employee_no"/>
<h:inputText id="empp" value="#{Bean.dto.empno}" >
<p:ajax event="keyup" update="empn,des,dep,loc" listener="#{Bean.Workerno}"/>
</h:inputText>
<h:outputText value="Employee_Name"/>
<h:inputText id="empn" value="#{Bean.dto.empname}" >
<p:ajax event="keyup" update="empp,des,loc,dep" listener="#{Bean.WorkerName}"/>
</h:inputText>
<h:outputText value="Department"/>
<h:inputText id="dep" value="#{Bean.dto.de}" />
<h:outputText value="Designation"/>
<h:inputText id="des" value="#{Bean.dto.de}" />
<h:outputText value="Employee_no"/>
<h:inputText id="empp" value="#{Bean.dto.empno}" >
<p:ajax event="keyup" update="empn,des,dep,loc" listener="#{Bean.Workerno}"/>
</h:inputText>
<h:outputText value="Employee_Name"/>
<h:inputText id="empn" value="#{Bean.dto.empname}" >
<p:ajax event="keyup" update="empp,des,loc,dep" listener="#{Bean.WorkerName}"/>
</h:inputText>
<h:outputText value="D"/>
<h:inputText id="dep" value="#{Bean.dto.d}" />
<h:outputText value="Designation"/>
<h:inputText id="des" value="#{Bean.dto.de}" />
BEAN
Your code looks fine...
One thing is change the ajax event from KeyUp to blur.
KeyUp - Forces to call the listener method for every typing letter.
Blur - Forces to call the listener method when you left the text box after typing the content.
Here you need 'blur' event because you need to call the DB after entered the all empno .
Make sure that the values are retrieved from DB, after changed the event
As for as my consern your DB value is not getting at workerNo() when ajax listener forces.
JSF Code
<h:ouputText value="empno"/>
<p:inputText id="empno" value="#{bean.empno}">
<p:ajax event="blur" listener="#{bean.ajaxEvent}" update="empname"/>
</p:inputText>
<h:ouputText value="empname"/>
<p:inputText id="empname" value="#{bean.empname}">
Bean Code
//Setters and getters of empname,empno
public void ajaxEvent()
{
if(getEmpNo()==//DB empno)
{
setEmpName("DB empname"); //Here only your updating the name field
}
}
I am trying to pass new date from PrimeFaces p:calendar (placed in p:dataTable column) to the backing bean:
<p:column >
<p:calendar value="#{bean.date}">`
<p:ajax />
</p:calendar>
</p:column>
It does not update bean.date. Variants with
<p:ajax update="#this" event="change"/>
<p:ajax update="#this" event="select"/>
do not update bean.date too. The only way I have found is using of listener. However, I suppose, there should be a way without listener implementation like for simple facelets:
<p:column>
<h:inputText value="#{bean.note}" >
<f:ajax/>
</h:inputText>
</p:column>
that works fine for me. Does anybody know how to get it working!?
<p:calendar value="#{Bean.value}">
<p:ajax update="display" event="dateSelect" listener="#{Bean.handleDateSelect}"/>
</p:calendar>
This should be helpful...but without listener means i dont think soo..you cant...
p:calendar it's a bit tricky for ajax; onSelectUpdate & selectListener are the key;
<p:column>
<h:inputText id="itDate" value="#{bean.note}" >
<f:ajax/>
</h:inputText>
</p:column>
and you must implement the handleDateSelect method in the Bean;
public void handleDateSelect(DateSelectEvent event) {
Date date = event.getDate();
setDate(date);
}