ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Date not repopulating properly on error (SAEF)

August 24, 2011 1:31am

Subscribe [2]
  • #1 / Aug 24, 2011 1:31am

    Disclaimer: I am a long time CI user but a novice when it comes to EE.

    I want visitors to book appointments which involves selecting a day and time. Since the times depend on the day (different hours on different days), I broke this operation up into two fields:

    appt_date with field type Date.
    time with field type Text.

    On submit I use javascript to combine the date (formated YYYY-MM-DD) with the time (formated hh:mm PM). I then store the result in appt_date and this works beautifully.

    My issue arises when there are any field errors and the form must be repopulated. Even though I’m using inline error handling, appt_date gets messed up and returns something like “1969-12-31 07:33 PM”. If I change appt_date from field type Date to Text, it repopulates properly.

    Some snippets of my code are below but more can be found here: http://pastebin.com/Ym5nfeVk
    If you are adventurous, all of the code can be found here: http://pastebin.com/BvydP6bN

    {exp:safecracker 
        id = "appointment-form" 
        return = "appointment/success" 
        channel    = "appointments" 
        datepicker = "no"
        error_handling = "inline"}
                 
        <!-- This is a datepicker that does displays the select date nicely to the user -->
        <label for="datepicker">Appointment Date</label>
        <input type="text" name="datepicker" id="datepicker" value="{appt_date}"/>
        
        <!-- Hidden input which is the actual appointment data/time. On submit, this get the correct format of YYYY-MM-DD hh:mm PM -->
        <input type="hidden" name="appt_date" id="appt_date" value="{appt_date}" />
        
        <!-- This will get its options for a JS function once a date has been selected using the date picker -->
        <label for="time">Time</label>
        <select name="time" id="time">
            <option>Select a date first</option>
        </select>
    {/exp:safecracker}

    Relevant JS:

    $( "#datepicker" ).datepicker({
            dateFormat: 'MM d, yy (DD)',     // Nicer looking date for users
            altField: '#appt_date',         // The real date 
            altFormat: 'yy-mm-dd',         // YYYY-MM-DD
            onSelect: function(dateText,insta){
                // Give time select its options
                changeHours(dateText);
            }
    });
    
    $('#submit').bind('click', function(e){
        
        // If a date has been picked (this will have a value)
        if ($('#appt_date').val()){
        
            // YYYY-MM-DD
            var day = $('#appt_date').val();
            
            // hh:mm PM
            var time = $('#time option:selected').text();
            
            // Combine them
            $('#appt_date').val( day + ' ' + time );
        }
        
        // Submit the form
        $('#appointment-form').submit();
    });

    I’ve been stuck on this for a while, any guidance would be greatly appreciated!

    - Justin

  • #2 / Aug 24, 2011 4:05am

    John Henry Donovan

    12339 posts

    Hi webfuel,

    This thread is more appropriate for the community help forum so moving it there

    I would probably add some inline validation with errors as opposed to relying on EE’s standard which will bring yo to a new page and you need to click a return button. That should hopefully keep your data and time intact

  • #3 / Aug 24, 2011 4:33am

    Hey John,

    Thanks for responding and putting this post in the proper forum (my bad!)

    Regarding inline validation, that is what I’m using… I guess it wasn’t clear from the pastebin dump. I’ll clean up my original post and the dump.

    - Justin

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases