yii: default value for date picker field and CJuiDatePicker

by prettyscripts on 2012-01-24 14:28:00

phpyii

setting a default date value to a date picker field is not so straightforward. as in, simply set a property for the CJuiDatePicker widget.

there are 2 ways to do it. these are based on some forum posts i found a while ago.

method 1 - give the attribute a default value

say, default the field with today.

in the action function in controller file, prior to render a view:

PHP:

function actionSomething() {
    ....
    $model->date_field date('Y-m-d'); // default to today
    $this->render(....);
}

method 2 - use html option

in the view file:

PHP:

...
<div class="row">
    <?php echo $form->labelEx($model,'date_field'); ?>
    <?php $this->widget('zii.widgets.jui.CJuiDatePicker'array(
        'model' => $model,
        'attribute' => 'date_field',
        'htmlOptions' => array(
            'value' => date('Y-m-d'), // set the default date here
        ),
    )) ?>
    <?php echo $form->error($model,'date_field'); ?>
</div>
...

note: line 8, set the default value.

Tags: date, default, php, yii

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)