简体   繁体   中英

How can I change the default value of date to null in Yii framework?

Do I need any change in the below code to get the Default value of DATE as NULL ? I have tried in the database, there I changed the constraint default to null, but I didn't find any solution. Would you please give me a solution ?

<?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
        ),
    )); ?>

Thanks in advance!

All data from form submit is available as string (or array of strings). So empty field is represented by empty string. If you want to change it to null you should use default validator, which will change empty fields to specified value:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM