Inline editor - default date

Inline editor - default date

arnorbldarnorbld Posts: 123Questions: 23Answers: 1

Hi all,

I have a table with start/end dates. I have this set up in the datatable as:

{"label": "Start Date", "name": "startdate", "type": "datetime"},
{"label": "End Date", "name": "enddate", "type": "datetime"},

This works and opens the date picker etc. But it always defaults to today's date rather than the date in the cell.

The dates show up in mm-dd-yyyy format. However when the edit field opens it shows the date in yyyy-mm-dd format.
In the backend PHP script I have the set/get:

Field::inst( 'startdate' )
    ->validator( Validate::dateFormat( 'Y-m-d' ) )
    ->getFormatter( Format::dateSqlToFormat( 'm-d-Y' ) )
    ->setFormatter( Format::dateFormatToSql('Y-m-d' ) ),

I suspect there is some discrepancy in the format somewhere. The date is saved correctly to the SQL table.

Appreciate any help on this :)

Replies

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    You can see the formatting properties for datetime with displayFormat and wireFormat - see example here. That might get you going, if not, please let us know,

    Colin

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi Colin,

    Thanks will check it out! What I noticed last night as I was banging on this was that IF I set the data to format as SQL in the table (yyyy-mm-dd) then the date picker picked up the right date and adjusted the date picker to it. So this is definitely related to the format.

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi Colin,

    Gosh, I had never scrolled down far enough on the examples page to see the "Dates and Time" section!!!<g> Thanks for making me scroll ;)

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Ha, that's given your finger some exercise for the day! :)

    Colin

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi Colin,

    Still no go on this! This table has TWO date columns (note only dates, no time involved, and this is stored as DATE in MySQL, not DATETIME)

    In the table, I have formatted the Start date as mm-dd-yyyy but the end date as SQL, yyyy-mm-dd:

    Start date has this in the fields in the editor configuration:

    {   label: "Start Date",
        name: "startdate",
        type: "datetime",
        def: function () { return new Date(); },
        displayFormat: 'MM-DD-YYYY',
        wireFormat: 'YYYY-MM-DD'
    },
    

    When I pop the date picker up for the start date I get:

    When I pop the date picker up for the end date I get:

    If I select a date from the date picker on the start date it shows the correct date in the correct format:

    If I accept it, it is saved correctly in the SQL database and the correctly formatted date is sent back.

    This is what I have in the PHP script for the editor for the start date:

    Field::inst( 'startdate' )
        ->validator( Validate::dateFormat( "d-m-Y" ) )
        ->getFormatter( Format::dateSqlToFormat( "d-m-Y" ) )
        ->setFormatter( Format::dateFormatToSql("Y-m-d" ) ),
    

    One thing to note: I construct the HTML table structure myself. I.e. the DataTable setup does NOT have an AJAX section to fetch the data.

    I'm not sure if that makes any difference as other fields that I have set up for example with a dropdown work perfectly. And the date picker works perfectly, it is ONLY that it doesn't recognize the date value as a valid date for some reason unless it's in yyyy-mm-dd format.

    The data in those two columns looks 100% identical for those two columns apart from the order of date/year:

    Thanks for any insights.

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi Colin,

    BTW: I tried this also with mm/dd/yyyy format - same thing - invalid date and the picker doesn't default to the right date.

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Use this for your PHP:

    Field::inst( 'startdate' )
        ->validator( Validate::dateFormat( "d-m-Y" ) )
        ->getFormatter( Format::dateSqlToFormat( "d-m-Y" ) )
        ->setFormatter( Format::dateFormatToSql("d-m-Y" ) ),
    

    I've changed the set formatter to indicate what the format is that is being sent from the client-side.

    And for the Editor field use:

        displayFormat: 'DD-MM-YYYY',
        wireFormat: 'DD-MM-YYYY'
    

    Note that in this case they are both the same, since you've done the formatting at the server-side already. And also they match the format that the server is using.

    Allan

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi Alan,

    OK, got this to work with d-m-Y and DD-MM-YYYY. Now the question is if it works with m-d-Y and MM-DD-YYYY for my American client ;)

  • arnorbldarnorbld Posts: 123Questions: 23Answers: 1

    Hi Alan,

    Changed things in the Editor configuration to:

    displayFormat: 'MM-DD-YYYY',
    wireFormat: 'MM-DD-YYYY'
    

    Changed the initial data to:

    $sdate->format('m-d-Y')
    

    And the Editor PHP to:

    Field::inst( 'startdate' )
        ->validator( Validate::dateFormat( "m-d-Y") )
        ->getFormatter( Format::dateSqlToFormat( "m-d-Y") )
        ->setFormatter( Format::dateFormatToSql("m-d-Y") ),
    

    And it's all working now :)

    This displays the date as mm-dd-yyyy in the table, and when the edit field opens. It also saves the data correctly in the database!

    Thank you so much for setting me straight on this!

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Hi,

    Great to hear you’ve got it working now!

    Allan

This discussion has been closed.