kartik-v / yii2-datecontrol

Date control module allowing separation of date formats for View & Model for Yii Framework 2.0.
http://demos.krajee.com/datecontrol
Other
55 stars 29 forks source link

Using in Grid filter does not set the value on the hidden input #112

Closed tsdogs closed 5 years ago

tsdogs commented 7 years ago

I don't know if it's related to #74 but it happens even if PJax is disabled.

When the DateControl widget is used in a grid filter when the date is changed (and the grid reload is triggered) the searchModel associated hidden input is not initialized and so the filter does not work.

I had to change the controller action to handle this situation as I was not able to find out why this is happening.

My solution was to write the following in the controller: (in the example the ModelSearch[field] hidden input is empty in the request->queryParams)

    public function actionIndex()
    {
        $params = Yii::$app->request->queryParams;
        if (isset($params['field-modelsearch-field'])) {
            $date = DateTime::createFromFormat('d/m/Y', $params['field-modelsearch-field'], new DateTimeZone('Europe/Rome'));
            if ($date && $date instanceof DateTime) {
                $params['ModelSearch']['field']=$date->format('Y-m-d');
            }
        }
        $searchModel = new ModelSearch();
        $dataProvider = $searchModel->search($params);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

tsdogs commented 5 years ago

Imho this should be fixed

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.