DateTime validation fails due to / instead of -

4 posts, 0 answers
  1. Leo
    Leo avatar
    7 posts
    Member since:
    Jan 2016

    Posted 01 Jun Link to this post

    I try to validate my model that has a date with fluentvalidation:
    RuleFor(x => x.Aanvang)
        .InclusiveBetween(
    new DateTime(2008,01,01), new DateTime(2100, 01, 01)).WithMessage("Wrong");


    web.config has:
    <globalization uiCulture="nl-NL" culture="nl-NL" enableClientBasedCulture="true" />

    The kendo culture stuff is in place:
    <script src="@Url.Content("~/Scripts/kendo/2016.2.504/kendo.aspnetmvc.min.js")"></script><br><script src="@Url.Content("~/Scripts/cultures/kendo.culture.nl-NL.min.js")"></script>

     Now when i enter 05-05-2015 in the datepicker, it throws the "Wrong" error. While it is accepted when i comment the fluentvalidator out. I figured that the gegenerated html is:

    <input data-val="true" data-val-date="The field Aanvang must be a date." data-val-range="Onwaarschijnlijk" data-val-range-max="01/01/2100 00:00:00" data-val-range-min="01/01/2008 00:00:00" data-val-required="'Aanvang' mag niet leeg zijn." id="Aanvang" name="Aanvang" type="text" value="1-1-0001" data-role="datepicker" class="k-input k-invalid" role="combobox" aria-expanded="false" aria-owns="Aanvang_dateview" aria-disabled="false" aria-readonly="false" data-bind="value:Aanvang" style="width: 100%;" aria-activedescendant="01ceca7f-c8ee-45ed-87b9-e1357df3799c_cell_selected" aria-invalid="true">

     

    That shows 01/01/2008 instead of 01-01-2008 format, nut sure if this is the problem, but it is differant and the only thing related thing i could find. Any advice?

     

    as

     

  2. Ianko
    Admin
    Ianko avatar
    1535 posts

    Posted 03 Jun Link to this post

    Hi Leo,

    Typically, with client-side validation, the data attributes are controlled and handled by native MVC utilities. More specifically, by using the data annotation attributes. In your case, FluentValidation is a replacement of the data annotation attributes and the data attributes should be handled as an out of the box feature as it is said here—https://github.com/JeremySkinner/FluentValidation/wiki/h.-MVC.

    As the data attributes on the client are fetched from the server, client-side globalization and culture changes of the Kendo scripts are not part of the issue here. 

    The dates in the attributes are controlled entirely by the FluentValidation logic. That said, I suggest you checking whether the FluentValidtion takes properly the culture on the server and properly assigns the values for the data attributes on the server. 

    Regards,
    Ianko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Leo
    Leo avatar
    7 posts
    Member since:
    Jan 2016

    Posted 03 Jun Link to this post

    I removed fluentvalidator and used normal data annotations:
    [Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")]

    The issue remained the same. The generated html is the same. It appears that it has nothing to do with the dash or slash. It looks like the validator internally does not respect the date cultere/format.
    I do read something about Globalize.js as plugin to jquery, but not sure how to implement that in combination with kendo scripts.

     

     

  5. Ianko
    Admin
    Ianko avatar
    1535 posts

    Posted 06 Jun Link to this post

    Hi Leo,

    jQuery globalization does not interfere with Kendo scripts and the way you setup both jQuery and Kendo culture and globalization have nothing specific. Basically, serve the globalization scripts in the right order jQuery recommends and after that serve the Kendo scripts. 

    Maybe this will help—http://blog.johnnyreilly.com/2012/09/globalize-and-jquery-validate.html.

    On a side note, I suggest you to firstly test without using Kendo scripts on the page to see what the native results are so to isolate the case in order to handle it more properly. After that, include Kendo scripts and continue with the required implementations. 

    Regards,
    Ianko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready