I have looked through about 50 forum posts concerning issues with "date picker", but have not found posts addressing the issue that I have had. The closest one I found was this one: DatePicker submit date onselect, but I am not sure it is exactly the same, so I am posting details about my issue and what I have used for a work-around. In brief, the problem that I have tried to solve is to use the CJuiDatePicker for picking a date and have the initial date displayed from the database in the text box associated with the CJuiDatePicker. From my initial testing it was an either/or - either use the CJuiDatePicker OR display the initial date from the database, but not both at the same time (assuming that I wanted to also save any changes back to the database after clicking the Submit button).
So the first thing I did was to use gii to create a model, crud and controller for a class that I called Myitem. It has several different types of fields. The one that is of interest here is the date_val field that is of type ‘date’ in the MySQL database. For the _form.php, by default the following text field is generated by gii:
<div class="row">
<?php echo $form->labelEx($model,'date_val'); ?>
<?php echo $form->textField($model,'date_val'); ?>
</div>
That works fine for displaying the current value from the database and for updating the Myitem fields in the database with a new date value when using keyboard text input.
So now I replace the date_val input box with the date picker widget and have the following.
<?php echo $form->labelEx($model,'date_val'); ?>
<?php
Yii::import('application.extensions.CJuiDatePicker.CJuiDatePicker');
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>Myitem::model(), // model object
'attribute'=>'date_val',
'value'=>$model->date_val,
'options'=>array('autoSize'=>true,
'dateFormat'=>'yy-mm-dd',
'defaultDate'=>$model->date_val,
'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
'buttomImageOnly'=>true,
'buttonText'=>'Select',
'showAnim'=>'fold',
'showOn'=>'button',
'showButtonPanel'=>true,
'yearRange'=>'1900',
),
'language'=>'en-AU',
));
?>
This code works fine for popping up a calendar widget and setting the value in the text box from the calendar widget and then saving it back to the database after submit. However, the problem is that the text box does not initially show the current value of the date_val from the database. The text box for the date_val associated with the widget is initially blank.
Now if I use the following code for the widget, using ‘name’ instead of the ‘model’ and ‘attribute’, then the text box DOES initially display the date value from the database in the text box (as in the default input box generated by gii, shown in the first code above).
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'date_val',
'value'=>$model->date_val,
'options'=>array('dateFormat'=>'yy-mm-dd',
'defaultDate'=>$model->date_val,
'yearRange'=>'1900',
),
'language'=>'en-AU',
));
However, in this case when I save a value that is selected from the widget, the new value does not get saved back to the database upon update.
But I want to both display the initial value from the database and save any new value back to the database upon update.
So what I have done is to create a workaround for the first case with the date picker widget. With the initial display of the form, I go ahead and display the date_val text box with the button for the date widget, and then use javascript to fill the value for the date_val textbox from the database. The code for the first part of this is as follows (same as above except I just add setting the $date_val).
<?php echo $form->labelEx($model,'date_val'); ?>
<?php
Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>Myitem::model(), // model object
'attribute'=>'date_val',
'value'=>$model->date_val,
'options'=>array('autoSize'=>true,
'dateFormat'=>'yy-mm-dd',
'defaultDate'=>$model->date_val,
'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
'buttomImageOnly'=>true,
'buttonText'=>'Select',
'showAnim'=>'fold',
'showOn'=>'button',
'showButtonPanel'=>true,
'yearRange'=>'1900',
),
'language'=>'en-AU',
));
$date_val = $model->date_val;
?>
And then at the end of the file I add the following javascript.
<script type="text/javascript">
function fillVals() {
// Set widget with actual value
var x = document.getElementById("Myitem_date_val");
x.value='<?php echo $date_val; ?>';
}
</script>
<script>
window.onload=fillVals();
</script>
I am pretty new to Yii, so this may be the long way around something that I am not setting correctly somewhere. If so, I would love to hear about a better way to do this. But if someone else is running into this same issue, I thought that it could be helpful to share what is working for me.