4

i'm using Yii2 extension - Jui DatePicker.

Is there a way to show by default the current timestamp in the text box, so the user does not need to input it every time it fills de form fields?

My view, and the form in Yii2:

 <?= $form->field($model, 'data')->widget(DatePicker::className(),


                [
                    'language' => 'en',
                    'inline' => false,
                    'clientOptions' =>[
                    'dateFormat' => 'yyyy-MM-dd',
                    'showAnim'=>'fold',
                    'yearRange' => 'c-25:c+0',
                    'changeMonth'=> true,
                    'changeYear'=> true,
                    'autoSize'=>true,
                    'showOn'=> "button",
                    'buttonText' => 'clique aqui',
                     //'buttonImage'=> "images/calendar.gif",
                    ]])

                ?>

In Yii1 i used to write the code like this (inside the widget), and the timestamp appeared by default:

'htmlOptions'=>array('size'=>12, 'value'=>CTimestamp::formatDate('d/m/Y')),

Is there an equivalent in Yii2?

Many thanks...

André Castro
  • 1,527
  • 6
  • 33
  • 60

1 Answers1

5

You can still use value:

<?= $form->field($model, 'data')->widget(DatePicker::className(),
    [
        'language' => 'en',
        ...
        'value' => date('Y-m-d'),

As noted in a comment (haven't confirmed this)

'value' => date('Y-m-d') parameter will work only if widget is used without model: DataPicker::widget(['value' => date('Y-m-d')])

Therefore you can use clientOptions to set defaultDate :

'clientOptions' => ['defaultDate' => date('Y-m-d')]     

Or better yet you can set $model->data to a default value of the current timestamp, either in your view or in the model.

if (!$model->data) $model->data = date('Y-m-d');
topher
  • 14,790
  • 7
  • 54
  • 70
  • 4
    `'value' => date('Y-m-d')` parameter will work only if widget is used without model: `DataPicker::widget(['value' => date('Y-m-d')])` – Tony Jun 23 '15 at 16:01
  • Many thanks, to you all. if (!$model->data) $model->data = date('Y-m-d'); Did the job very well. – André Castro Jun 23 '15 at 19:31