Completed
Last Updated: 29 Jul 2024 14:32 by John
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
6 comments
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