Moeez
Moeez

Reputation: 478

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.

Upvotes: 0

Views: 1150

Answers (2)

Ravenous
Ravenous

Reputation: 1160

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.

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133370

You should assigne a value realted to your nodel->field and assigne/reassigne the value you need after (or before ) subimit

<?=
    DateTimePicker::widget([
        'name' => 'datetime_10',
        'value' => $yourModel->yourField, // <------ this 
        '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,
        ]
    ]);
 ?>

Upvotes: 0

Related Questions