0

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!

Binar Web
  • 867
  • 1
  • 11
  • 26
  • Maybe try `value` attribute outside `htmlOptions` like there https://stackoverflow.com/a/45649715/6350167 – ArtOsi May 07 '18 at 08:07
  • The default value inside `htmlOptions` is used to populate the input with the date you set. This will not do this for the database, you should add it as a rule in your model file. – Brainfeeder May 07 '18 at 08:12
  • You can find out [all about rules and validation](https://www.yiiframework.com/wiki/56/reference-model-rules-validation?revision=16#how-validation-works) in the docs. – Brainfeeder May 07 '18 at 08:18
  • Thanks @ArtOsi for your quick reply... – Kumar Satya May 07 '18 at 09:08
  • Actually I want the Null value in the database, so for this should I need to change anything here or change in Database part . @ArtOsi – Kumar Satya May 07 '18 at 09:09
  • Can you please help me by giving the code for this ? @Brainfeeder – Kumar Satya May 07 '18 at 09:41

1 Answers1

2

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],
    ];
}
rob006
  • 21,383
  • 5
  • 53
  • 74