Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nik Webb 1 post 71 karma points
    Aug 28, 2018 @ 11:05
    Nik Webb
    0

    Datepicker display issue

    Hi,

    I am using the default date picker on Umbraco forms, when you select a date it displays correctly as expected (16 August 2018), however when using pages going either previous or next, when you return to the page with the date on it the date is not displaying correctly (16 January 0008).

    Date originally selected: Date selected using pikaday

    When you return to the page: enter image description here

    Although the value is correct when you inspect the element it is not displaying correctly in the textbox. Has anyone else had this issue or know how to resolve?

    Umbraco version: 7.7.4

    Umbraco Forms version: 6.0.4

    English UK is set to our default Language

  • Allan Hawkey 232 posts 303 karma points
    Jan 30, 2019 @ 13:40
    Allan Hawkey
    0

    Hi Nik, just wondered if you ever got this fixed as I've hit the same issue. Thanks

  • Allan 42 posts 192 karma points
    Jan 31, 2019 @ 17:13
    Allan
    0

    Hi Allan (good name and correct spelling) and Nik,

    Maybe not a 100% fix, but possibly better than what we have currently which is very confusing for the user and if they don't check it will save incorrectly.

    I settled for clearing the field if the date does not validate. That way the user has to enter it again but at least it will be correct... yeah?

    You need to find the FieldType.DatePicker.cshtml file (probably in Partials > Forms > Themes > Default > Fieldtypes - but yours may differ) and re-work it a bit like this...

    @model Umbraco.Forms.Mvc.Models.FieldViewModel       
    @using Umbraco.Forms.Mvc
    
    @{
        string val = string.Empty;
        if (Model.ValueAsObject != null && Model.ValueAsObject != "")
        {
            val = Model.ValueAsObject.ToString();
        }
    }
    
    <input type="hidden" name="@Model.Name" id="@Model.Id" class="datepickerfieldshadow" value="@val"/>
    <input type="text" class="@Html.GetFormFieldClass(Model.FieldTypeName) text datepickerfield" name="@Model.Name" id="@(Model.Id)_1" value="@val" @{if (Model.Mandatory) { <text> data-val="true" data-val-required="@Model.RequiredErrorMessage" </text> }} autocomplete="off"/>
    <script>
        // Function to check a valid date...
        function isValidDate(s) {
            var bits = s.split('/');
            var d = new Date(bits[2] + '/' + bits[1] + '/' + bits[0]);
            return !!(d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[0]));
        }
        // Check it is correct on focus/blur
        $('.datepickerfield').blur(function () {
            if (!isValidDate($(this).val())) {
                $(this).val(null);
            }
        });
        // Check it is correct on load
        $('.datepickerfield').each(function () {
            if (!isValidDate($(this).val())) {
                $(this).val(null);
            }
        });
    </script>
    

    I am still looking at this and may get to a better solution, but for now I think it kind of solves the problem.

  • Allan 42 posts 192 karma points
    Feb 01, 2019 @ 09:53
    Allan
    0

    Think I cracked it replace that last bit of javascript for this...

    $('.datepickerfield').each(function (i) {
        if (!isValidDate($(this).val())) {
            var shadow = $('.datepickerfieldshadow:eq(' + i + ')').val();
            if (shadow!='') {
                var bits = shadow.split('-');
                $(this).val(bits[2] + '/' + bits[1] + '/' + bits[0]);
            }
            else {
                $(this).val(null);
            }
        }
    });
    

    So if we have a hidden value stored we just cut up the bits and put them back correctly OR make the field null.

Please Sign in or register to post replies

Write your reply to:

Draft