Yii2 kartik 日期时间选择器在页面提交后保留值

Yii2 kartik datetime picker retain value after page submitting

我在我的 yii2 项目中使用 yii2-widget-datetimepicker。我想在提交页面后保留它的价值。

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

如何保留选定的日期时间值?任何帮助将不胜感激。

您应该为您的 nodel-> 字段分配一个实际值,并且 assigne/reassigne 在提交之后(或之前)您需要的值

<?=
    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,
        ]
    ]);
 ?>

无需进一步调整,您可以做到

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

但这并不可靠。如果你在多个页面收集数据,你真的应该考虑storage/persistence,至少在所有表单完成后会话并保存到数据库,或者跳过会话并直接使用内存中的数据,数据库等