Activeform Datefield And Default Value

I’m using a date field in some form :


public function actionCreate() {

        $model = new AccountingEntry;

        $model->entry_date = date('d-m-Y');

[...]

}




    <div class="row">

        <?php echo $form->labelEx($model, 'entry_date'); ?>

        <?php echo $form->dateField($model, 'entry_date'); ?>

        <?php echo $form->error($model, 'entry_date'); ?>

    </div>



Anyway, I can’t have the entry_date field set to ‘today’ as default value.

How could I achieve this ?

Dear Friend

You want today’s date or the word “today” get displayed?.

The following will definitely displays the date in dateform.




public function actionCreate() {

        $model = new AccountingEntry;

        $model->entry_date = date('Y-m-d'); //this format ensures that it is compatible with database storage.

[...]

}



You want the word "today" get displayed then placeholder is an option.

Then in controller, we have to comment out our date assignment.




public function actionCreate() {

        $model = new AccountingEntry;

        //$model->entry_date = date('Y-m-d');

[...]

}






<div class="row">

        <?php echo $form->labelEx($model, 'entry_date'); ?>

        <?php echo $form->dateField($model, 'entry_date',array('placeholder'=>'today')); ?>

        <?php echo $form->error($model, 'entry_date'); ?>

    </div>



Then we can register a script in a following way in the view.




Yii::app()->clientScript->registerCoreScript('jquery.ui');

Yii::app()->clientScript->registerScript('today','

$("#AccountingEntry_entry_date").focus(function(){

	$(this).val($.datepicker.formatDate("yy-mm-dd", new Date()));

	

	});

');




I hope I helped a bit.

Regards.

Hi Seenivasan, and thanks for the reply !

The problem was the date format you spotted : frech format does not initialize the dateField but the standard ‘Y-m-d’ works fine. :)

Interesting insights also with that placeholder attribute. I’ll keep the information at hand.

Best regards,

H.