Search code examples
datewickettextfieldmodels

Wicket 8 DateTextField (from wicket-extension)


I have a simple Java class, let's name it Person: it contains, as the name suggests, information regarding a person. So it has properties like name, surname, address, sex, phone number and my culprit, the birthday. I have a page that allows users to edit these fields: it consists of a autocompleting textfield in which users can input the Person ID, or part of it, and choose the Person they want to edit from a drop-down list. The rest of the page is a list of text fields tied with Wicket models to the Person that the users chooses from the list: when this happens, every textfield gets filled correctly with the properties of the Person object chosen. Short example of the java representing the textfields and their model:

private Person person;
[...]
    formPatient = new Form("patientForm", new PropertyModel<>(this, "person"));
[...]
    formPatient.add(tmp = new MyTextField<>("nome", new PropertyModel<>(this, "person.nome")));
    formPatient.add(tmp = new MyTextField<>("secondoNome", new PropertyModel<>(this, "person.secondoNome")));
    formPatient.add(tmp = new MyTextField<>("cognome", new PropertyModel<>(this, "person.cognome")));
    DropDownChoice sessoDDC = new DropDownChoice("sesso", new PropertyModel<>(this, "person.sesso"), Arrays.asList(Enums.Sex.values()));
    formPatient.add(sessoDDC);
    formPatient.add(dtf = new DateTextField("bday", new PropertyModel<Date>(this, "person.bday"), "dd/MM/yyyy"));
[...]

This works fine, as I said. But, for the birthday field, I need to use a text field with a calendar picker. It can be easily arranged, just by changing the related HTML markup from

<input style="display: table-cell;" type="text" wicket:id="bday"/> 

to

<input style="display: table-cell;" type="date" wicket:id="bday"/> 

in order to have a nice and easy-to-use picker to choose the date from.

The problem is that, when the HTML type is set to "date", the auto-updating feature stops working: the DateTextField is always showing the default "dd/MM/yyyy" text, and not a real date. All the other fields still get updated when the user chooses a Person from the list (even the dropdown list representing sex), so the Wicket model mechanism is still working. I tried overriding methods, both of the PropertyModel and the DateTextField, in order to see if at some point the model object connected to the DateTextField gets lost or something, but it's always there, although it's not represented. I also discovered that the final HTML is like this:

<input style="display: table-cell;" type="text" wicket:id="bday" value="01/01/2011" name="bday">

so the date information is there, even if the textfield itself seems unable to show it.

Any help is appreciated. If you need more info or more pieces of code, just let me know.


Solution

  • According to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date the value should use - as a delimiter between the month, day and year.