Search code examples
typo3extbasetypo3-6.1.x

Why doesn't eval null not work in the TCA


I have a field defined as such:

max_items int(11) NULL

If you leave this field empty in the backend I want it to store NULL.

For this I use this configuration in the TCA, which doesn't work:

'max_items' => array(
    'exclude' => 0,
    'label' => '...',
    'config' => array(
        'type' => 'input',
        'eval' => 'null',
    ),
),

Edit: Instead of storing the expected value NULL, it stores 0. I tried max_items int(11) DEFAULT NULL, but that didn't work aswell.

Edit2: Thanks freshp! I ended up writing my own eval function:

<?php
class tx_myextension_evalfunc {
    function evaluateFieldValue($sValue, $aIsIn, &$bSet)
    {
        return ($sValue === '') ? null : $sValue;
    }
}
?>

Using this configuration:

'max_items' => array(
    'exclude' => 0,
    'label' => '...',
    'config' => array(
        'type' => 'input',
        'eval' => 'tx_myextension_evalfunc',
    ),
),

Solution

  • There was a bug in older TYPO3 versions:

    The current implementation in the TYPO3 backend does not allow to store NULL values, only empty strings or zero as number are allowed.

    This bug is fixed in TYPO3 6.0 and higher. In the TCA config there is a new eval option "null":

    'config' = array(
      'type' => 'input',
      'eval' => 'null',
      ...
    );
    

    If this option is enabled, there is a checkbox at the right side of the input. If it's deactivated, the NULL value is saved to the database, if it is activated, an integer value can be entered.

    If you want to have the checkbox per default deactivated to store Null as default, add 'default' => null to it:

    'config' = array(
      'type' => 'input',
      'eval' => 'null',
      'default' => null,
    );
    

    Tested in TYPO3 8 LTS. Then it looks like this:

    enter image description here


    Original answer:

    There are two interesting links for you: