Wicket DateTextField with <input type="date"> not working

634 Views Asked by At

I would like to have HTML <input type="date"> component with date picker and so on.

I cant make it work with Wicket. I need to change the model value on onChange() event without Submit Form and I need to show the initial value in input field of the java.util.Date object from my Model, which is not working too.

My code java:

IModel<Date> model = new Model<Date>() {
    private Date date = new Date();

    public Date getObject() {
        return date;
    }

    public void setObject(Date object) {
        this.date = object;
    }
};
DateTextField dateTo = new DateTextField("date", model);
dateTo.add(new OnChangeAjaxBehavior() {

    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        System.out.println(model.getObject()); <-- it is still null
    }
});
add(dateTo);

HTML markup:

<input type="date" wicket:id="date" />

As you can see, date is initialize as new Date(), but in <input type="date"> I can see only dd.mm.yyyy and same as setObject() method. The Date object param is always null after change.

What am I doing wrong? Thanks for any answer.

1

There are 1 best solutions below

0
On

The HTML date input fields send the data back to the server in the format 2012-12-30. So you need to configure the Wicket DateTextField to parse that format:

import org.apache.wicket.extensions.markup.html.form.DateTextField;

new DateTextField("date", model, "yyyy-MM-dd");

See also: https://stackoverflow.com/a/9519493/2511197