0

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.

Altair
  • 325
  • 3
  • 16
  • Hi, have you tried with dateTo.getValue instead of model.getObject() ? – Andrea Del Bene Mar 24 '20 at 12:14
  • Hi, getValue() is undefined. I think, that value of "input type date" should be in "Date date" object, so getObject() should return the frontend value after onChange and same as it should show the "Date date" object with new Date() after initialization, but these things dont work. – Altair Mar 24 '20 at 12:28

1 Answers1

0

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

rec
  • 10,340
  • 3
  • 29
  • 43