Well. I find in all places and I still without solution. I need to do a input mask with Primefaces and bind the value to a Date object in my bean.
The problem is: I use a converter with all validations, convertions and formats with my own code. My answer is: Is other solution with better performance. I hope so. Please help.
P/D: I don't need use a pickdate. I need the input mask to do this
I use this:
<div style="margin-bottom:1em;font-size: 1.2em;">
<p:inputMask id="dob" mask="99/99/9999" value="#{viewMB.request.dateOfBirth}" style="width:8em;" >
<f:convertDateTime pattern="MM/dd/yyyy" />
</p:inputMask>
<p:watermark value="MM/DD/YYYY" for="dob" />
</div>
and you can still add custom validator if you wish.
Well, this is my solution
<h:form>
<p:inputMask mask="99-99-9999" value="#{mask.date}" converterMessage="Invalid Date!" converter="dateconverter" />
<h:commandButton actionListener="#{mask.submit()}" value="Submit" />
</h:form>
And the converter
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
System.out.println(value);
if (value != null) {
try {
if (validateDate(value)) {
return convertDate(value).toDate();
}
else{
throw new ConverterException("Invalid date!");
}
} catch (Exception e) {
throw new ConverterException("Invalid date!");
}
}
throw new ConverterException("Null String!");
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
System.out.println(value);
if (value != null) {
try {
Date now = (Date)value;
DateTime d = new DateTime(now);
return d.getDayOfMonth() + "-" + d.monthOfYear() + "-" + d.getYear();
} catch (Exception e) {
throw new ConverterException("Convertion failure!");
}
}
throw new ConverterException("Null object!");
}
private boolean validateDate(String param) throws ParseException{
//Param is a date format from input mask
String[] values = param.split("-");
int day = Integer.valueOf(values[0]);
int month = Integer.valueOf(values[1]);
int year = Integer.valueOf(values[2]);
DateTime converted = convertDate(param);
if (converted.getDayOfMonth() != day || converted.getMonthOfYear() != month || converted.getYear() != year) {
return false;
}
else{
return true;
}
}
private DateTime convertDate(String param) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(param);
return new DateTime(convertedCurrentDate);
}
The Managed Bean
@ManagedBean(name="mask")
public class MaskBean {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void submit(){
RequestContext context = RequestContext.getCurrentInstance();
context.execute("alert('date submited: value recibed " + date.toString() + "')");
}
}
It work for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With