Wiquery DatePicker displays time, validation fails
-
21-02-2021 - |
Pregunta
I'm having problems with the Wiquery DatePicker in the following code in a Wicket page (using a CompundPropertyModel, the date property is of type java.util.Date):
DatePicker<Date> datePicker = new DatePicker<Date>("date"){
@Override
public boolean isVisible() {
return selectedType.hasDate();
}
};
datePicker.setDateFormat(DateUtil.DATE_PATTERN); // pattern is "dd.MM.yyyy"
form.add(datePicker);
The HTML this is bound to:
<input wicket:id="date" id="date"/>
The problem is that when editing existing data, the input field displays the time along with the date, and when submitting the form, validation fails because this does not fit the pattern.
How can I get the DatePicker to display the current value correctly?
No hay solución correcta
Otros consejos
This seems to be a bug in the relatively obscure Wiquery DatePicker component. I've switched to org.apache.wicket.extensions.yui.calendar.DatePicker
, which does not have this problem.
Being the DatePicker
component a TextField
, why not use a custom IConverter
in it to return only dd.MM.yyyy
in its convertToObject
?
I haven't been able to see browsing its sources any registered IConverter
or other way to convert input, so this will probably be conflicting with whatever is formatting input in this component.
UPDATE
After debugging this in a quickstart using WiQuery 1.2.4 and Wicket 1.4.17, it shows that the initial value of the DatePicker
(which is a TextField
) is the standard conversion performed by Component.getDefaultModelObjectAsString()
.
Since the TextField
has a IModel<Date>
, it will use whatever IConverter
is registered for the Date
class. In your case it might be using a custom IConverter
that's formatting with the time. I'd try overriding DatePicker
's getConverter()
and using a SimpleDateFormat
that honours the format specified in setDateFormat()
.
This issue gives a hint on that an IConverter
should be specified along with the DatePicker
: Issue 168: Invalid (or uncommon) date format for NL in DatePicker
You might also find this discussion on the Wicket users list useful: DatePicker to pick a year. Julien Roche (one of the owners of the project) states there that setDateFormat
only works on the client side with JQuery:
I think you have to set the right converter on your wicket textfield (with the override of the method getConverter and with the class PatternDateConvert). The "dateFormat" option works only on the client side with jQuery.
first take a look at https://cwiki.apache.org/WICKET/using-custom-converters.html
then you know that wicket convert you object to a text from that mechanic so.. all you have to do
Override newConverterLocator() method in Application class to provide custom ConverterLocator.
protected IConverterLocator newConverterLocator() {
ConverterLocator converterLocator = new ConverterLocator();
converterLocator.set(Date.class, new DateConverter());
return converterLocator;
}
tip: watch out with java.sql.Date class converter