Search code examples
phpyii2bootstrap-datetimepickerkartik-v

Yii2 kartik datetime picker retain value after page submitting


I am using yii2-widget-datetimepicker in my yii2 project. I want to retain the value of it after submitting the page.

<form action="index" method="post" >


<?=
                DateTimePicker::widget([
                    'name' => 'datetime_10',
                    'options' => [
                            'placeholder' => 'Start Date Time',
                            'autocomplete' => 'off',
                        'required' =>true,
                            ],
                    'convertFormat' => false,
                    'pluginOptions' => [
                        'format' => 'yyyy-mm-dd hh:i:ss',
                        //'startDate' => '01-Mar-2014 12:00 AM',
                        'todayHighlight' => true,
                        'autoclose' => true,
                    ]
                ]);
                ?>
                <?=
                DateTimePicker::widget([
                    'name' => 'datetime_11',
                    'options' => [
                            'placeholder' => 'End Date Time',
                            'autocomplete' => 'off',
                            'required' =>true,
                            ],
                    'convertFormat' => false,
                    'pluginOptions' => [
                        'format' => 'yyyy-mm-dd hh:i:ss',
                        //'startDate' => '01-Mar-2014 12:00 AM',
                        'todayHighlight' => true,
                        'autoclose' => true,
                    ]
                ]);
                ?>
</form>

How to retain the selected date-time value? Any help would be highly appreciated.


Solution

  • Without further tweaks, you could do

    <form action="index" method="post">
        <?=
        DateTimePicker::widget([
            'name'          => 'datetime_10',
            'value'         => Yii::$app->request->post('datetime_10', null),
            'options'       => [
                'placeholder'  => 'Start Date Time',
                'autocomplete' => 'off',
                'required'     => true,
            ],
            'convertFormat' => false,
            'pluginOptions' => [
                'format'         => 'yyyy-mm-dd hh:i:ss',
                //'startDate' => '01-Mar-2014 12:00 AM',
                'todayHighlight' => true,
                'autoclose'      => true,
            ]
        ]);
        ?>
        <?=
        DateTimePicker::widget([
            'name'          => 'datetime_11',
            'value'         => Yii::$app->request->post('datetime_11', null),
            'options'       => [
                'placeholder'  => 'End Date Time',
                'autocomplete' => 'off',
                'required'     => true,
            ],
            'convertFormat' => false,
            'pluginOptions' => [
                'format'         => 'yyyy-mm-dd hh:i:ss',
                //'startDate' => '01-Mar-2014 12:00 AM',
                'todayHighlight' => true,
                'autoclose'      => true,
            ]
        ]);
        ?>
    </form>
    

    But this is not reliable. If you are gathering data throughout multiple pages, you should really consider storage/persistence, at least session & save to database after all the forms are completed, or skip session and go directly with in-memory data, a database etc.