CJuiDatePicker : defaultDate not working

I call the widget this way:


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

            

            'name'=>'start_time',

            // additional javascript options for the date picker plugin

            'options'=>array(

                'showAnim'=>'fold',

                'dateFormat'=>'dd/mm/yy',

                'defaultDate'=>'3/3/2011', // It's not working

            ),

            'htmlOptions'=>array(

                'style'=>'height:20px;',

                'value'=>'3/3/2011', // It's not working either

            ),

        )); ?>

but I can’t set the Default date.

The options => defaultDate is kind of working: it sets the date “internally”, so when I click the text field I get the calendar starting on the stated date, but I want it to DISPLAY the default date, so the user knows that something is already set and doesn’t worry the date was lost in the update process.

For this reason I tried setting the "value" in htmlOptions but the parameter is simply ignored. What am I doing wrong?

It’s easy when you use a model …




<?php

$model->start_time = '3/3/2011';  // default date

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

            'model'=>$model,

            'attribute'=>'start_time',

            // additional javascript options for the date picker plugin

            'options'=>array(

                'showAnim'=>'fold',

                'dateFormat'=>'dd/mm/yy',

            ),

            'htmlOptions'=>array(

                'style'=>'height:20px;',

            ),

        )); 

?>



Without a model, I think it’s better not to use CJuiDatepicker wrapper.




<?php

echo CHtml::textField('start_time', '3/3/2011');

Yii::app()->clientScript->registerScript('start-time-datepicker',

'$("#start_time").datepicker({

  showAnim: "fold",

  dateFormat: "dd/mm/yy"

});'

);



[EDIT]

Also the following code worked for me.




<?php

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

            'model'=>$model,

            'attribute'=>'start_time',

            // additional javascript options for the date picker plugin

            'options'=>array(

                'showAnim'=>'fold',

                'dateFormat'=>'dd/mm/yy',

            ),

            'htmlOptions'=>array(

                'style'=>'height:20px;',

                'value'=>'3/3/2011',

            ),

        )); 

?>



thank you very much softark!

I am new in yii.I am using CJuiDateTimePicker to pick calender date and time.But when i select date from date and calender date picker and submit it shows "Start Time cannot be blank.".The code is below.My table field name is socstd and using gii crud functionality of yii.

/////////code////////////

<?php echo $form->labelEx($model,‘Start Time:’); ?>

<?php Yii::import(‘application.exten​sions.CJuiDateTimePicker.CJuiD​ateTimePicker’);

$this->widget(‘zii.widgets.jui​.CJuiDatePicker’, array(

‘name’=>‘socstd’,

// additional javascript options for the date picker plugin

‘options’=>array(

‘showAnim’=>‘fold’,

),

‘htmlOptions’=>array(

‘style’=>‘height:20px;’

),

));

?>

<?php echo $form->error($model,‘socstd’);​ ?>

You’re not even using the code in softark’s answer… And your code is certainly not the one generated by gii.

Take it step by step. Use the code generated by gii: does it work?

Then modify it accordingly, and understand the goal of each modification.

in ../framework/zii/widgets/jui/CJuiDatePicker on line 97 $this->options[‘defaultDate’] getting overridden. It said:

[indent]$this->options[‘defaultDate’]=$this->value;[/indent]

Putting there this line instead will make it work.

[indent]$this->options[‘defaultDate’]=(!empty($this->options[‘defaultDate’]))? $this->options[‘defaultDate’]: $this->value;[/indent]

As Lube points out, the defaultDate is being overwritten, so to answer the OP, all you need to do is put ‘value’ in the top level of the properties array, NOT under ‘htmlOptions’ – see below (and note that you ought to use leading zeros since that’s what you specified with dd and mm in dateFormat)

@Lube – Well, yes and no. In general, the idea of a default is to supply a value when the user-supplied value is missing. Your fix should read

[indent]$this->options[‘defaultDate’]= !empty($this->value) ? $this->value : $this->options[‘defaultDate’];[/indent]

However, technically (IMHO), defaultDate SHOULD NOT have to be overwritten with the value at all, for the reason stated above. Unfortunately, as far as I can tell from the way jQuery’s datepicker is written, an inline instance “incorrectly” (by my reasoning) gets its initial value from defaultDate, whereas it should get it from some other data source, such as the hidden field that CJuiDatePicker produces. The trouble is, attaching datepicker to a field implies (according to jQuery’s logic) that the field is selectable, and a hidden field is obviously not, and of course it also makes the datepicker NOT inline :(. So maybe datepicker should read the initial value from an HTML5 data-* attribute in the div or span tag. Something to ask the gurus at jQuery.org

As far as Yii is concerned in the mean time, CJuiDatePicker probably should be refactored to call datepicker(‘setDate’, $(’$id’).val()) via jQuery’s ready function when flat is true.