validate dateformat OR set to null if empty
validate dateformat OR set to null if empty
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
This discussion has been closed.
Answers
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.
Its the
ifEmpty
formatter that you want here.Allan
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
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'sdate_format
if there is valid data.Allan
cool.
this seems to work ok for 1.5.3