PHP Date validation.

PHP Date validation.

classic12classic12 Posts: 228Questions: 60Answers: 4

Been searching a while but cannot work it out.

My dates are stored in a Msql table as 2017-12-11

So I need to show them in the tables as
11-12-2017 ( ie DD-MM-YYYY)

Also the editor using the date type: 'datetime'

Field::inst( 'start_date' )
            ->validator( 'Validate::dateFormat', array(
                "format"  => Format::DATE_ISO_8601,
                "message" => "Please enter a date in the format yyyy-mm-dd"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
    )

What is the correct syntax plaes.

Cheers

Steve Warby

This question has an accepted answers - jump to answer

Answers

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    Nearly there guys.

    I found this link https://datatables.net/forums/discussion/26818/date-dd-mm-yyyy

    client side

    {
                    label: "Inv Date:",
                    name: "invoice.invDate",
                    //type:   'datetime',
                    def: function () { return new Date(); },
                    type: "date",
                    dateFormat: 'dd-mm-yy'
                }
    

    server side

    Field::inst( 'invoice.invDate' )
                ->validator('Validate::dateFormat', 'd-m-Y')
                ->getFormatter('Format::date_sql_to_format', 'd-m-Y')
                ->setFormatter('Format::date_format_to_sql', 'd-m-Y'),
    

    This is working and I can change the date manually and save.
    But I don't have the 'calendar' popup.

    If I change type to datetime I get the calendar but when I update it says the format is in correct.

    Which piece of the jigsaw am I not understanding here guys.

    Cheers

    Steve Warby

  • classic12classic12 Posts: 228Questions: 60Answers: 4
    Answer ✓

    Just added the moments.js to my project and when I create a new invoice I can now use:

             editorInvoices.on('initCreate', function () {
              editorInvoices.set( 'invoice.CustID', selectedCustomerID );
              var today = moment().format('DD-MM-YYYY');
              editorInvoices.set( 'invoice.invDate',  today);
              editorInvoices.set( 'invoice.payDate',  '');
              editorInvoices.disable(["invoice.CustID"]);
              editorInvoices.disable(["invoice.InvID"]);
            });
    

    Cheers

    Steve Warby

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Thanks for posting back - good to hear you've got it now.

    Allan

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    Still got this

    But I don't have the 'calendar' popup.

    If I change type to datetime I get the calendar but when I update it says the format is in correct.

    Which piece of the jigsaw am I not understanding here guys.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    For d-m-Y on the server-side try:

                    type: "datetime",
                    format: 'DD-MM-YYYY'
    

    The Moment documentation for the formatting is available here.

    Allan

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    I have this on the server side.

    $selectedCustomerID = $_POST["selectedCustomerID"];
    
    include( "DataTables.php" );
    
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'invoice' , 'InvID')
        ->fields(
            Field::inst( 'invoice.InvID' ),
            Field::inst( 'invoice.CustID' ),
            Field::inst( 'invoice.invDate' )
                ->validator('Validate::dateFormat', 'd-m-Y')
                ->getFormatter('Format::date_sql_to_format', 'd-m-Y')
                ->setFormatter('Format::date_format_to_sql', 'd-m-Y'),
            Field::inst( 'invoice.payDate' )
                ->validator('Validate::dateFormat', 'd-m-Y')
                ->getFormatter('Format::date_sql_to_format', 'd-m-Y')
                ->setFormatter('Format::date_format_to_sql', 'd-m-Y'),
            Field::inst( 'invoice.delMethod' ),
            Field::inst( 'invoice.payMethod' ),
            Field::inst( 'invoice.invNett' ),
            Field::inst( 'invoice.invTax' ),
            Field::inst( 'invoice.invTotal' ),
            Field::inst( 'invoice.vatRate' ),
            Field::inst( 'invoice.orderNum' ),
            Field::inst( 'invoice.notes' )
                )
        ->where('invoice.CustID', $selectedCustomerID, '=')
        ->process( $_POST )
        ->json();
    

    On the editor I have

    {
                    label: "Inv Date:",
                    name: "invoice.invDate",
                    //type:   'datetime',
                    def: function () { return new Date(); },
                    type: "datetime",
                    dateFormat: 'd-m-y' // tried dateFormat: 'dd-mm-yy'
                }
    



    So how to I get the popup calendar to set the date to DD-MM-YYYY

    Cheers

    Steve Warby

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Change:

                    type: "datetime",
                    dateFormat: 'd-m-y' // tried dateFormat: 'dd-mm-yy'
    

    to be:

    type: "datetime",
    format: 'DD-MM-YYYY'
    

    datetime uses format not dateFormat.

    Allan

This discussion has been closed.