This is a migrated thread and some comments may be shown as answers.

DateInput Required Validation

4 Answers 502 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Travis
Top achievements
Rank 1
Travis asked on 09 Oct 2018, 06:50 PM

When using DateInputFor on a required nullable datetime it allows submit when date is blank. It should throw a field required error like DatePicketFor. Same error if you use DatePickerFor(...).DateInput().

 

using System;
using System.ComponentModel.DataAnnotations;
namespace WebApplication9.Models {
    public class TestViewModel {
        [Required]
        [DataType(DataType.Date)]
        public DateTime? TheDate { get;set;}
        [Required]
        [DataType(DataType.Date)]
        public DateTime? TheDate2 { get; set; }
    }
}

 

@model TestViewModel
<form asp-action="Index" method="post">
    <script type="text/javascript">
        $.validator.setDefaults({
            ignore: ""
        });
    </script>
    <p>
        @(Html.Kendo().DatePickerFor(m => m.TheDate))
        <span asp-validation-for="TheDate" class="text-danger"></span>
    </p>
    <p>
        @(Html.Kendo().DateInputFor(m => m.TheDate2))
        <span asp-validation-for="TheDate2" class="text-danger"></span>
    </p>
    <input type="submit" value="Go" />
</form>

 

 

4 Answers, 1 is accepted

Sort by
0
Travis
Top achievements
Rank 1
answered on 11 Oct 2018, 01:59 PM
Since I have not received a reply yet I have submitted a support ticket  1346150.
0
Konstantin Dikov
Telerik team
answered on 12 Oct 2018, 10:08 AM
Hello Travis,

I am posting the answer from the support ticket for others with similar question:
"The required validation will not work with enabled DateInput for the DatePickers or for the DateInput editor and this is due to the rendered mask in the input, which is what the DataInput is designed for. I have to say that the only option for applying required validation would be to add custom validation and compare the current value in the input with the format specified in the DateInput (or the default one)"


Best Regards,
Konstantin Dikov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Travis
Top achievements
Rank 1
answered on 12 Oct 2018, 01:32 PM

Here is the start of a solution I came up with yesterday. Will probably need to add some more checks for partially entered dates and will post a follow up when I have a more robust solution. Something like this https://stackoverflow.com/questions/7760032/how-to-validate-if-string-is-date-with-jquery

I really think this should be an out of the box feature.

 

1.$.validator.addMethod("extrarequired", function (value, element) {
2. 
3.    return !(value === 'month/day/year');
4. 
5.}, "Requested Completion Date is Required");
0
Konstantin Dikov
Telerik team
answered on 17 Oct 2018, 09:40 AM
Hello Travis,

Thank you for sharing the solution with the community.

As for a built-in solution, the validation is taking into account the value in the input and handling such required validation for the DateInput will require rendering second input element, which is a breaking change that we try to avoid.


Regards,
Konstantin Dikov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Date/Time Pickers
Asked by
Travis
Top achievements
Rank 1
Answers by
Travis
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or