model state validation for DateTime Input

6 posts, 0 answers
  1. Bina
    Bina avatar
    2 posts
    Member since:
    Jun 2017

    Posted 25 Jun Link to this post

    I am using the Kendo DateInput inside my razor views as below

     <span>Date of Birth</span>
        @(Html.Kendo().DateInput().Name("Patient.Dob").Format("dd-MMM-yyyy").Min(new DateTime(1900, 1, 1))
                 .Max(DateTime.Today).Messages(m => m.Year("yyyy").Day("dd").Month("mmm")))
     
    When the date of birth is not entered, the model state validation fails as it tries to validate the placeholder value - ("dd-MMM-yyyy")

    How can i specify the placeholder, such that it does not do model state validation
  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2087 posts

    Posted 28 Jun Link to this post

    Hi Bina,

    You should either make the DateTime filed nullable or set the Required attribute in the model for that field:
    [DisplayName("Last supply")]
    [DataType(DataType.Date)]
    public DateTime? Dob
    {
        get;
        set;
    }
     
    or
     
    [Required]
    [DisplayName("Last supply")]
    [DataType(DataType.Date)]
    public DateTime Dob
    {
        get;
        set;
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Bina
    Bina avatar
    2 posts
    Member since:
    Jun 2017

    Posted 28 Jun in reply to Konstantin Dikov Link to this post

    Thanks for your response.

    I have already set the DateTime field as nullable as below

            [Display(Name = "Date of Birth")]
            [Column("DOB", TypeName = "date")]
            [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
            public DateTime? Dob { get; set; }

     

    The problem is that, during the postback, the raw value available in the model is dd-mmm-yyyy and hence the modelState.Valid returns false in the Controller method.

    Note: the model holds the value null. But the raw value is dd-mmm-yyyy. model validation error states that the format dd-mmm-yyyy is not a valid date.

  4. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2087 posts

    Posted 03 Jul Link to this post

    Hi Bina,

    Could you please open a regular support ticket and attach a sample, runnable project replicating the issue, because on my local tests I was not able to replicate the issue in question?


    Best Regards,
    Konstantin Dikov
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. S
    S avatar
    1 posts
    Member since:
    Jul 2017

    Posted 01 Aug Link to this post

    I'm issuing the same problem. What might cause the problem?

     

     

     

  6. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1966 posts

    Posted 03 Aug Link to this post

    Hello,

    We would need a sample runnable sample project to investigate it locally. Since you are not able to attach projects in the forum I would suggest to open a support ticket and attach the project in the ticket. 

    Regards,
    Boyan Dimitrov
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top