converter and selectlistner in calendar

UI Components for JSF
Post Reply
vishuvrk
Posts: 2
Joined: 24 Jan 2012, 06:53

24 Jan 2012, 07:52

Hi
I am using primegaces 2.2 with mojarra2.03
I'm trying to implement the following.
1. Two calender fields named 'From Date' and 'To Date' (both popup mode and MM/dd/yyyy pattern)
2. Whenever user selects a date from the date picker, for 'From date' same should be copied to 'To Date'. Same thing should happen even when user manually enters the date.
4. As soon as the user enters first 2 digits representing month, a / should be appended. And a second / after date is entered.
3. Perform proper validation so that whenever user manually enters 15/02/2012 ( as it is not a valid date in MM/dd/yyyy), an error message to be displayed.

I tried with custom validator but in validate method i get the date object as (Mar 02 2013 ...) instead of getting a string 15/02/2012. So using setLenient to false will not help. So I'm using custom converter. Now the problem is both selectListner and f:converter will not work simultaneously. ie when converter is present copy of 'From Date' to 'To Date' wont work and when f:converter is not added, selectListner will work but i cannot do the proper validation.

in my xhtml
<p:calendar id="calendar1" value="#{myBean.fromDate}" showOn="button" selectListener="#{myBean.handleDateCopy}" onSelectUpdate="calendar2" onkeyup="appendSlashes(this)" onblur="document.getElementById('myForm:calendar2_input').value = this.value">
<f:converter converterId="dateConverter" for="calendar1_input" />
</p:calendar>

<p:calendar id="calendar2" value="#{myBean.toDate}" showOn="button"
onkeyup="appendSlashes(this)">
</p:calendar>

In MyBean
public void handleDateCopy(DateSelectEvent event){
setToDate(event.getDate());
}

In faces-config.xml
<converter>
<converter-id>dateConverter</converter-id>
<converter-class>com.sample.converter.DateConverter</converter-class>
</converter>

My custom Converter
public class DateConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent component, String value) {
System.out.println("inside getAsObject");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date inputDate;
try {
sdf.setLenient(false);
inputDate = sdf.parse(value);
} catch (ParseException ex) {
FacesMessage message = new FacesMessage();
System.out.println("in excep");
message.setDetail("Please Enter valid Date");
message.setSummary("Please Enter valid Date");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ConverterException(message);
}

return inputDate;
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
System.out.println("inside getAsString");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
return sdf.format(arg2);
}
}

syamganna
Posts: 15
Joined: 03 Jan 2012, 18:03

27 Jan 2012, 23:56

Why dont you use js for this instead of writing converters and all.
I just simply did this.

<p:calendar widgetVar="leaseExpirationDateTo" onchange="validateDate(this);" onfocus="jQuery('input.hasDatepicker').mask('99/99/9999');"
value="#{leaseSearchController.leaseExpirationDateTo}" navigator="true"
pattern="MM/dd/yyyy" id="leaseExpirationDateTo"
>
</p:calendar>

vishuvrk
Posts: 2
Joined: 24 Jan 2012, 06:53

28 Jan 2012, 20:00

@syamganna
Thanks a lot for your reply. I did not use JS as the requirement is to do the server side validation and to show the error messages using FacesMessage.
Any help in this direction will be of great help.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests