Issue with InputDialog and TypedDatePicker

Is it a issue or am I doing something wrong?
I open an InputDialog with a DatePicker to select a date, but the dialog returns a Date type value instead of LocalDate.

        dialogs.createInputDialog(this)
                .withHeader("Input dialog")
                .withParameters(
                        InputParameter.parameter("passedDate")
                                .withRequired(true)
                                .withDatatype(datatypeRegistry.get(LocalDate.class))
                                .withField(() -> {
                                    TypedDatePicker<LocalDate> datePicker = uiComponents.create(TypedDatePicker.class);
                                    datePicker.setValue(LocalDate.now());
                                    return datePicker;
                                })
                ).withActions(DialogActions.OK_CANCEL)
                .withCloseListener(closeEvent -> {
                    if (closeEvent.getCloseAction().equals(InputDialog.INPUT_DIALOG_OK_ACTION)) {
                        Object passedDate = closeEvent.getValue("passedDate");
                        if ( ! (passedDate instanceof LocalDate) ) {
                            log.info("Passed date has {} class , must be LocalDate", passedDate.getClass().getName());
                        }
                    }
                }).open();

Hello!

InputParameter#withField() does not propagate attributes from InputParameter like “datatype”, “required”, “requiredMessage” and “defaultValue” to the created field.

Change your code to the following:

InputParameter.parameter("passedDate")
        .withField(() -> {
            TypedDatePicker<LocalDate> datePicker = uiComponents.create(TypedDatePicker.class);
            datePicker.setDatatype(datatypeRegistry.get(LocalDate.class));
            datePicker.setValue(LocalDate.now());
            datePicker.setRequired(true);
            return datePicker;
        })

Or if you don’t need the field customization you can do the following:

InputParameter.parameter("passedDate")
        .withRequired(true)
        .withDatatype(datatypeRegistry.get(LocalDate.class))
        .withDefaultValue(LocalDate.now())

I’ve create an issue in the documentation: Add notes about how field supplier works in InputDialog · Issue #637 · jmix-framework/jmix-docs · GitHub

1 Like