如何在Yii框架中将date的默认值更改为null?

时间:2018-05-07 08:01:36

标签: php yii

我是否需要对以下代码进行任何更改才能将DATE的默认值设为NULL? 我在数据库中尝试过,我将约束默认值更改为null,但我没有找到任何解决方案。 你能给我一个解决方案吗?

<?php 
        // $model->confirmation_dt ='03/03/2011';  // default date
        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
        'model' => $model,
        'attribute' => "confirmation_dt",
        'options'   => array(
            'dateFormat' => Yii::app()->params['dateFmtDP'],
            'yearRange'  => '1900:c+10',
            'changeYear' => true
        ),
        'htmlOptions' => array(
            'size' => '10',         // textField size
            'maxlength' => '10',    // textField maxlength
            // 'value' => '03/03/2011', // Alternate method for default date
            // 'value' => date('d/m/Y'), // set the default date as today's date
            // 'value' => NUll, // set the default date here // This one is not working
        ),
    )); ?>

提前致谢!

1 个答案:

答案 0 :(得分:2)

表单提交的所有数据都以字符串(或字符串数​​组)的形式提供。所以空字段由空字符串表示。如果您想将其更改为null,则应使用default验证程序,这会将空字段更改为指定值:

public function rules() {
    return [
        // ...
        ['confirmation_dt', 'default', 'value' => null],
    ];
}