Null/0/0000-00-00 Dates

Null/0/0000-00-00 Dates

jacob.steinbergerjacob.steinberger Posts: 86Questions: 18Answers: 1

Trying to get Editor to work with null or "0" dates and haven't had any success. I'm finding old posts where this seems to be a problem and suggests fixes that aren't included in the most recent version of Editor. Specifically:

https://datatables.net/forums/discussion/16632/editor-optional-date-field

Or

public static function dateFormat( $val, $data, $opts ) {
    $format = is_array($opts) ? $opts['format'] : $opts;
 
    if ( $val === '' ) {
        return true;
    }
 
    $date = date_create_from_format($format, $val);
    if ( ! $date ) {
        return isset( $opt['message'] ) ?
            $opts['message'] :
            "Date is not in the expected format";
    }
    return true;
}

The instruction to check if the value is empty isn't in the current release. Examples all require a non-null non-0 date.

Asking generically, does Editor support empty/null/0 dates for fields that are optional?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    Editor's built in datetime field type should fully support empty strings and null values. It won't support a 0 since it treats anything it sees as a string and attempts to deformat it, but null and empty string should be okay.

    What is the behaviour that you are seeing?

    Allan

  • jacob.steinbergerjacob.steinberger Posts: 86Questions: 18Answers: 1
    edited February 2016

    Specifically,

    DateTime::__construct(): Failed to parse time string (0) at position 0 (0): Unexpected character

    I've tried 0000-00-00 and null dates, neither are handled well by Editor.

    I've tried removing the validator and all manner of formatters passed to the get/set function. I've started digging into the dateFormat and Validator functions themselves to try to get to the heart of the issue, but if Editor supports what I'm trying to do, then I have to question if I've done something wrong.

    I understand that the exact error is a PHP error thrown with when doing something bad with dates. Still tracking down the exact problem area.

  • jacob.steinbergerjacob.steinberger Posts: 86Questions: 18Answers: 1
    edited February 2016

    As a side note, even the examples when you manually enter 0000-00-00 it doesn't pass the validator.

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    The Editor simple example appears to work okay with an empty string (null is written to the database). Try creating a new row with just a first and last name.

    Are you using the Editor 1.5.5 PHP libraries (not just the 1.5.5 JS)?

    Allan

  • jacob.steinbergerjacob.steinberger Posts: 86Questions: 18Answers: 1

    It sure does, but the specific date picker example doesn't:

    https://editor.datatables.net/examples/dates/dates.html

    Now to figure out what's different.

    I have a fresh download of everything. Editor.php shows 1.5.5.

  • jacob.steinbergerjacob.steinberger Posts: 86Questions: 18Answers: 1

    Ok, I think we can mark your previous comment as the answer. My error, in the end, was me doing something wrong as I kind of expected. I had a date validator for a non-date field (basically I set it for one row lower than I should have). Once this was corrected, everything worked.

    Still a bit odd that the specific date picker example doesn't take nulls, even though the backend php looks the same/similar.

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Different database table - and one which isn't configured to allow null values in those columns.

    Good to hear you've got it sorted out now!

    Allan

This discussion has been closed.