How to put default value in CJuidatepicker in Yii?

扶醉桌前 提交于 2020-01-16 01:16:09

问题


I have the code below to display a calendar input in Yii form.

<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name' => 'publish_date',
                'attribute' => 'publish_date',
                'model'=>$model,
                'options'=> array(
                  'dateFormat' =>'yy-mm-dd',
                  'defaultDate' => '2014-3-4',
                  'altFormat' =>'yy-mm-dd',
                  'changeMonth' => true,
                  'changeYear' => true,
                  'appendText' => 'yyyy-mm-dd',
                ),  
              )); 
 ?>

The default value work on the calendar but I want to show it by default in the calendar input too when the form is rendering.

How can I do it?


回答1:


You can use Html value attribute for this

<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name' => 'publish_date',
                'attribute' => 'publish_date',
                'model'=>$model,
                'options'=> array(
                  'dateFormat' =>'yy-mm-dd',
                  'defaultDate' => '2014-3-4',
                  'altFormat' =>'yy-mm-dd',
                  'changeMonth' => true,
                  'changeYear' => true,
                  'appendText' => 'yyyy-mm-dd',
                ),
               'htmlOptions'=>array('value'=>'2013-4-4')
              )); 



回答2:


Manquer's solution didn't work (in version 1.1.17), CJuiDatePicker extends from CJuiInputWidget and it has a value property which is used by CHtml::inputField() when rendering the element, overwriting the value field of htmlOptions property.

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name' => 'publish_date',
    'attribute' => 'publish_date',
    'model'=>$model,
    'options'=> array(
        'dateFormat' =>'yy-mm-dd',
        'defaultDate' => '2014-3-4',
        'altFormat' =>'yy-mm-dd',
        'changeMonth' => true,
        'changeYear' => true,
        'appendText' => 'yyyy-mm-dd',
    ),
    'value' => '2013-3-4',
));


来源:https://stackoverflow.com/questions/23779397/how-to-put-default-value-in-cjuidatepicker-in-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!