André Castro
André Castro

Reputation: 1565

Yii2 - jui datepicker extension - display current timestamp by default

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...

Upvotes: 4

Views: 9004

Answers (1)

topher
topher

Reputation: 14860

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');

Upvotes: 5

Related Questions