Saving Date Range Picker to Session

I am pretty new to Yii, but I am making a lot of headway on developing a dashboard application that is pulling from an ERP for reporting. I am writing a startDate and endDate to $session on login and I use this to control a date range throughout the application. What I am trying to do now is update these values with Kartik DateRangePicker. I have the widget configured and it pulls it’s start input and end input from these value. Can someone steer me in the right direction on creating a model and/or controller to handle the ability to change these values from the widget?

Here is my existing widget configuration:


<?=

    DateRangePicker::widget([

    'name'=>'dateRange',

    'useWithAddon'=>false,

    'presetDropdown' =>true,

    //'hideInput' => true,

    'convertFormat'=>true,

    'startAttribute' => 'from_date',

    'endAttribute' => 'to_date',

    'startInputOptions' => ['value' => isset($_SESSION['startDateTime']) ? $_SESSION['startDateTime'] : null],

    'endInputOptions' => ['value' => isset($_SESSION['endDateTime']) ? $_SESSION['endDateTime'] : null],

    'pluginOptions'=>[

        'locale'=>['format' => 'm/d/Y'],

    ]

]);

?>

Just take a look at following section of the guide.

Working with Forms

http://www.yiiframework.com/doc-2.0/guide-start-forms.html

Ok. Here is where I am at:

Model:


<?php


namespace app\models;


use Yii;

use yii\base\Model;


class DaterangeForm extends Model

{

    public $dateTimeRange;

    public $startDateTime;

    public $endDateTime;


    public function rules()

    {

        return [

            [['startDateTime', 'endDateTime'], 'required'],

            [['startDateTime', 'endDateTime'], 'date'],

            [['dateTimeRange'], 'safe']

        ];

    }

}

Controller Action:


public function actionTest()

    {

       $model = new DaterangeForm();


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {


            Yii::$app->session->set('startDateTime', $this->startDateTime);

            Yii::$app->session->set('endDateTime', $this->endDateTime);


            return $this->goBack();

        } else {

            return $this->render('test', [

                'model' => $model,

            ]);

        }

    }

View:




<?=


	DateRangePicker::widget([

		'model' => $model,

		'attribute' => 'dateTimeRange',

		'useWithAddon'=>false,

		'presetDropdown' =>true,

		'convertFormat'=>true,

		'startAttribute' => 'startDateTime',

		'endAttribute' => 'endDateTime',

		'pluginOptions'=>[

		    'locale'=>['format' => 'Y/m/d'],

		]

	]);

?>



[list=1]

[*]I need to populate the initial values of the range from the current $session. (The documentation states that it is not best practice to call the session from a model). How do I do this?

[*]When I apply the date range, the widget updates and closes out. How do I trigger it to automatically submit so it will validate and update the session with the new values?

[/list]

I think this will do:




public function actionTest()

    {

       $model = new DaterangeForm();

       $model->startDateTime = Yii::$app->session->get('startDateTime');

       $model->endDateTime = Yii::$app->session->get('endDateTime');


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {


            Yii::$app->session->set('startDateTime', $this->startDateTime);

            Yii::$app->session->set('endDateTime', $this->endDateTime);


            return $this->goBack();

        } else {

            return $this->render('test', [

                'model' => $model,

            ]);

        }

    }



Sorry, but I can’t tell since I’m not familiar with this extension. But probably you have to set “pluginEvents” property and write some javaScript function that executes ajax call.

http://demos.krajee.com/date-range