DatePicker for nullable DateOnly

2 Answers 58 Views
DatePicker
Marco
Top achievements
Rank 2
Iron
Iron
Iron
Marco asked on 27 Feb 2024, 11:39 AM | edited on 27 Feb 2024, 11:41 AM

Hy,

I have an EditForm for inserting a Model Dto. 

The Dto Model has two dates. The start date which is of type DateOnly and the end date which is of type DateOnly nullable.

Although the end date is null, the date picker sets the value "01/01/2001" by default and I can't find a way to enter a null value using this component because if I try I get the error: "The date format is not parsable. Please enter valid date".

This is my code:

Can anyone tell me if this is a feature that has yet to be implemented or if I'm doing something wrong?

I attach photos of the edit form.

Thank you

2 Answers, 1 is accepted

Sort by
0
Svetoslav Dimitrov
Telerik team
answered on 29 Feb 2024, 09:41 AM

Hello Marco,

I would suggest that you remove the Field parameter for all <FormItem> tags that have <Template>. If this does not help you move forward, can you modify the code snippet below so that the issue you are facing is reproducible?

@using System.ComponentModel.DataAnnotations

<TelerikForm Model="@Model">
    <FormValidation>
        <DataAnnotationsValidator />
    </FormValidation>

    <FormItems>
        <FormItem>
            <Template>
                <TelerikDatePicker T="DateOnly?" @bind-Value="@Model.Date"></TelerikDatePicker>
            </Template>
        </FormItem>
    </FormItems>
</TelerikForm>

@code {
    public SampleData Model { get; set; } = new();

    public class SampleData
    {
        public DateOnly? Date { get; set; }
    }
}

Regards,
Svetoslav Dimitrov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Marco
Top achievements
Rank 2
Iron
Iron
Iron
commented on 06 Mar 2024, 11:28 AM | edited

Hy Dimitrov,

If I remove the Field parameter from the tag FormItem the DatePicker works.

But I want create the FormItem (for the Date property) without using the Template tag. Like this:

 

The StartDate property in my Dto is declared as follows:

The problem is that even if the date is null, by default it ONLY VISUALLY shows me this value:

In fact, if I try to press Save button without touching the DatePicker, it shows me the DataAnnotations error message (for the required attribute that i put) because I believe that the date continues to have the NULL value:

I hope I explained myself better,
I'm waiting for a response

Regards..

Svetoslav Dimitrov
Telerik team
commented on 08 Mar 2024, 01:59 PM

We have an open bug report that covers the scenario when you use DateOnly? and leave the form to autogenerate the form editors - The Form autogenerated date picker with nullable DateOnly / TimeOnly value shows '1/1/2001' / '12:00 AM' instead of 'M/d/yyyy' / 'h:mm aa' like the standard TelerikDatePicker. I have added your Vote for it and you can click the Follow button to receive email notifications on status updates. 
0
Marco
Top achievements
Rank 2
Iron
Iron
Iron
answered on 04 Mar 2024, 07:17 AM

Hello Dimitrov,

I tried removing the Field property from all FormItems that have a template and the DatePicker now works correctly.

Tags
DatePicker
Asked by
Marco
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Svetoslav Dimitrov
Telerik team
Marco
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or