DateTimePicker - only date or time selected

Hi,

using jmix 2.2, I have complains about working with date time picker. When only date or only time is selected and entity is saved then no validation warning is shown and that specific field is set to null.

I have requirement that some kind of warning or validation should be shown to user when this happens and it should prevent user to save entity.

I did not find any way to do that in jmix. All value events etc are fired after selecting both. So I was considering extending jmix component but as I am debugging it, I dont see a way how to do that. Maybe you can give me an advise or introduce this feature in future release?

Thank you,
Jakub

If entity field should always be not null add NotNull annotaion to it. Bean Validation :: Jmix Documentation

If entity field should be not null only in one screen add NotNullValidator to screen field Validator :: Jmix Documentation

Hi yarik1706,

Thanks for you reply, but requirement is little bit different.

It is in case when field is not required/mandatory. It can be null, but user wants to fill it for specific entity.

User will select only date, and forgets to select time, then he confirms screen with no warning or validation and it is saved ok, but datetime is null. Problem is he thinks now that date is filled but it is not. It is still null.

So idea is to add some warning when user fills only date/time.

I know it is kinda problem of user that he is not able to fill both fields and thinks its ok :slight_smile: . But true is it is not much user friendly and atleast some warning would be nice. Like “you filled one of this thing but you have to fill both or it will not be saved” . I already spent some unnecessary debugging time to only find out that datetime is not filled and it is because of this.

My second idea how to solve this problem is to set only time to default 00:00 and leave date part empty. But I think jmix does not offer this either.

Thanks for any more suggestions,
Jakub

Hello,

I have the same problem … Jmix 2.2.3

image

Until the time portion is filled by the user, this
TypedDateTimePicker<OffsetDateTime> afterDateTimeField
evaluates as null.
I tried to see if I can add some valuechange listener to fill the time part with a default value, e.g. 12:00 ,
but timePicker and datePicker fields of superclass DateTimePicker are private.

It would be nice to have the ability to either set a default time when the date part is entered or, even better warn that the time part needs to be entered too by the user.
The way it works now, the user selects a date and continues, but in reality, it is as if the user did not do anything.

Kind regards,
Mladen

Hi, @mbucan,

The Jmix TypedDateTimePicker component extends the Vaadin Flow DateTimePicker component. The Vaadin DateTimePicker component does not provide an API to set a value for the time, it is possible to set a date with time only.

But here the workaround is described.

I have tried this solution in Jmix application with version 2.2.3 and it works.

My component described in the view descriptor:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<view xmlns="http://jmix.io/schema/flowui/view"
      title="msg://blankView.title">
    <layout>
        <dateTimePicker id="dateTimePicker"/>
    </layout>
</view>

Workaround implementation in the view controller:
The code described below sets a time as “10:00” if the date value is changed.

    @ViewComponent
    private TypedDateTimePicker<Comparable<LocalDateTime>> dateTimePicker;

    @Subscribe
    public void onInit(final InitEvent event) {
        dateTimePicker.getElement().executeJs("this.__datePicker.addEventListener('change', function(){this.__timePicker.value='10:00';}.bind(this));");
    }

Hope it helps.

Regards,
Maria.

2 Likes

Dear Maria,

thank you very much for this solution. Not only did you give me fish, but you also taught me how to catch fish, as I will be looking more to Vaadin resources from now on.

Kind regards,
Mladen

3 Likes

Thanks @m.orlova , it works fine. I created mine custom component extending TypedDateTimePicker that is adding only this oneliner and works fine.

But it is little but annoying as I have to think about it all the time and every new generated screen have to be edited. I simply easily will forget about it and then some fields will have not this feature.

Dont you consider to implement this as defaultTime (or whatever name) attribute into TypedDateTimePicker ? Just idea to consider as I think this is normal requirement to have for dateTimePicker component in general.

Thank you,
Jakub

1 Like