validate dateformat OR set to null if empty

validate dateformat OR set to null if empty

crush123crush123 Posts: 417Questions: 126Answers: 18

i have an editor instance which uses a datapicker and is working fine.
i would now like to give the user the option of clearing the date field and storing a null value.

    Field::inst( 'tblorders.GCCustomerChargedDate' )
        ->validator( 'Validate::dateFormat', array(
            'empty' => true,
            'format' => 'j M Y'
        ) )
        ->getFormatter( 'Format::datetime', array(
            'from' => 'Y-m-d',
            'to' =>   'j M Y'
        ) )
        ->setFormatter( 'Format::datetime', array(
            'from' => 'j M Y',
            'to' =>   'Y-m-d'
        ) ),

i have changed the validator to now allow an empty value, but the datetime field stores this as 0000-00-00, how can i force it t store empty value as null ?

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited March 2018

    From memory, I think it's a combination of enabling a default value of null in your database,
    and using a "null if empty" validator in your Editor.

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    edited March 2018

    Its the ifEmpty formatter that you want here.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    i am using a legacy version in this instance (1.5.3)

    I tried adding the legacy version of setFormatter as follows, which now sets a null when i submit an empty string but sets 0000-00-00 when i set any other date value

        Field::inst( 'tblorders.GCCustomerChargedDate' ) 
            ->validator( 'Validate::dateFormat', array(
                'empty' => true,
                'format' => 'j M Y'
            ) )
            ->getFormatter( 'Format::datetime', array(
                'from' => 'Y-m-d',
                'to' =>   'j M Y'
            ) )
            ->setFormatter( 'Format::datetime', array(
                'from' => 'j M Y',
                'to' =>   'Y-m-d'
            ) )
            ->setFormatter( 'Format::nullEmpty' ),
    
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Ah yes - sorry. You can't have multiple set formatters, even in the latest version. Currently what you need to do is create a custom formatter, one which will return null if there is an empty string input, or use PHP's date_format if there is valid data.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    cool.

    this seems to work ok for 1.5.3

        Field::inst( 'tblorders.GCCustomerChargedDate' ) 
            ->validator( 'Validate::dateFormat', array(
                'empty' => true,
                'format' => 'j M Y'
            ) )
            ->getFormatter( 'Format::datetime', array(
                'from' => 'Y-m-d',
                'to' =>   'j M Y'
            ) )
            ->setFormatter( function ( $val ) {
                if ($val == '') {
                    return null;
                } else {
                    return date( 'Y-m-d', strtotime( $val ) );
                }
            } ),
    
This discussion has been closed.