Help with allowing blank dates


When I bring up my form for editing, the date field defaults to 0000-00-00

If I blank it out, it saves ok, but when I try and edit the record again, it defaults to 0000-00-00

If I leave it at 0000-00-00, I get a date error.

Here is my validatior.

array(‘FDATE’, ‘type’, ‘type’=>‘date’, ‘dateFormat’=>‘yyyy-MM-dd’, ‘allowEmpty’ =>‘true’, ‘message’=>‘Date Created must be a date.’),

$this->widget('zii.widgets.jui.CJuiDatePicker', array(

'name'=>'Prjmaster[FDATEUPD]', 'id'=>Prjmaster_FDATEUPD, 'value'=>$model->FDATEUPD,

// additional javascript options for the date picker plugin









Thanks in advance.

The only solution I found to work is in the controller.


if ($model->FDATE == ‘0000-00-00’)

$model->FDATE = null;

No one has any ideas?

I view my fix as a hack.

I do not understand why the dates are showing up as 0000-00-00

I see they are stored in MySql that way.

Is there an easy fix for this?


If the user does not enter a date, I want to store it as empty, not 0000-00-00



I cannot test right now… but I think that mysql inserts 0000-00-00 as an empty date… other option is to use null…

Anyway wou will need to convert dates from display format to mysql format and back… there where post about that on this forum…

Thanks, I will try and find them.

Had similar problem when using dateFormat, which inserted 0000-00-00 in DB even though form field had correct date format value

Solution: Add this to the Model

protected function beforeSave()


            $this->FDATE = date('Y-m-d', CDateTimeParser::parse($this->FDATE, 'dd-MM-yyyy'));

        return true;


protected function afterFind()


            $this->FDATE = Yii::app()->dateFormatter->formatDateTime(CDateTimeParser::parse($this->FDATE, 'yyyy-MM-dd hh:mm:ss'), 'short', false);

        return true;