I’m using Yii’s zii.widgets.jui.CJuiDatePicker and have the following problem:
It works well when inserting a new record, or when updating a record with a NULL date.
But when I try to update a record with a date value, I can’t change it.
Do you know what may be happening?
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'datetime',
'name' => $model->datetime,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
'altFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'1920:2010',
'showOn'=>'both',
'buttonText'=>'...' ),
'htmlOptions'=>array( 'style'=>'height:20px;' ),
) );
Although the field is named datetime it’s actually a date MySQL field.
I’m assuming in your update action you have something like this:
Now, if you check the source of the generated html for your form view, you’ll see that the id and name of the input element for your
datetimeattribute is set as the value of that attribute, which actually happens due to the line'name'=>$model->datetime. For example it could end up being something like:Whereas what we want is:
So that our controller action can assign the value of the attributes using the line
$model->attributes = $_POST['DateModel'];.Hence a simple solution is not to use the
'name'attribute of the CJuiDatePicker widget.Edit:
Otherwise if you absolutely need the ‘name’ attribute, you have to set the date field in your controller action, like this: