Completed
Last Updated: 12 Mar 2025 12:42 by ADMIN
Release 2024 Q2 (May)
Stefan
Created on: 05 Oct 2022 08:27
Category: DateTimePicker
Type: Bug Report
47
DatePicker, TimePicker and DateTimePicker should accept null value as valid when bound to nullable DateTime

Hello,

A Date/Time Picker is bound to a nullable DateTime value. If the user clears the value with the keyboard, the component will show as invalid (with red border). Instead, the picker should treat null values as valid.

Here is a test page with a few different workarounds:

<TelerikButton OnClick="@( () => DateValue = null )">Clear Value</TelerikButton>
<TelerikButton OnClick="@( () => DateValue = DateTime.Now )">Set Value</TelerikButton>

DateValue.HasValue: @DateValue.HasValue

<p>Default behavior:</p>

<TelerikDatePicker @bind-Value="@DateValue"
                   Width="200px" />

<p>Red border will disappear after blur (CSS):</p>

<TelerikDatePicker @bind-Value="@DateValue"
                   Width="200px"
                   Class="@( !DateValue.HasValue ? "valid-null" : "" )" />

<p>Red border will never appear (CSS):</p>

<TelerikDatePicker @bind-Value="@DateValue"
                   Width="200px"
                   Class="valid-null" />

<p>Red border will disappear after blur (OnChange):</p>

<TelerikDatePicker @bind-Value="@DateValue"
                   Width="200px"
                   OnChange="@OnPickerChange" />


<style>
    .k-input.k-invalid.valid-null {
        border-color: rgba(0, 0, 0, 0.08);
    }
</style>

@code {
    private DateTime? DateValue { get; set; } = DateTime.Now;

    private async Task OnPickerChange(object newValue)
    {
        DateTime? newDate = (DateTime?)newValue;

        if (!newDate.HasValue)
        {
            DateValue = DateTime.Now;
            await Task.Delay(1);
            DateValue = null;
        }
    }
}

Duplicated Items
10 comments
ADMIN
Dimo
Posted on: 12 Mar 2025 12:42

Hello Piotr,

Hm, you are right, sorry about that. The problem persists in two specific cases and I opened a new bug report here, which includes a detailed description and a suggested workaround.

Regards,
Dimo
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Piotr
Posted on: 12 Mar 2025 07:39

Thank you for the reply but I'm using Clear button on a DateTimePicker; adding ShowClearButton to the REPL example and using it shows red outline over a nullable field.

ADMIN
Dimo
Posted on: 12 Mar 2025 07:33

Hi Piotr,

Yes, the feature is implemented. I assume that you are clearing just one date segment, which still triggers the invalid state. The component treats this as ambiguous user behavior. Instead, you need to clear all segments, so it's certain that your intent is to clear the value rather than edit it.

Regards,
Dimo
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Piotr
Posted on: 12 Mar 2025 07:05
Has this really been implemented? Still seeing the same problem on 8.0
John
Posted on: 29 Jul 2024 14:32
Thanks for the workaround. The DateRangePicker has this issue as well so I tweaked this solution a bit to work with the DateRangePicker as well. I agree that this should've worked out of the box when binding to Nullable DateTimes.
David
Posted on: 20 Jun 2023 17:48
Thanks for the workarounds.

"null" should be valid for a NullableDateTime and this should be the default behavior.
Hannes
Posted on: 15 Jun 2023 10:09
That should be implemented!
Dean
Posted on: 26 Jan 2023 16:57
Thanks for the workaround, plus 1 for this becoming the default behaviour
Stef
Posted on: 26 Oct 2022 08:28

Came across the same problem, thanks for the workaround.

I also added this to my CSS override workaround.


  .k-input.k-invalid.valid-override:focus-within {
        box-shadow: 0 0 0 0.25rem rgba(27, 110, 194, 0.25);
    }
 
Norbert
Posted on: 21 Oct 2022 08:44
I Hope this will be implemented as it is very helpful once we want to have some default state into the grid when searching, reverting it back