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?
<?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,
]);
}
}
[*]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?
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.