CGridView Date Range Without Cookies or Session (External Filtering)


This wiki article has not been tagged with a corresponding Yii version yet.
Help us improve the wiki by updating the version information.

Here's a tutorial on how to create a CGridView with external date filters the trick is to create hidden columns within the CGridView Filters something like in the example below: grid

first we have to create a Class File for the column that we will put our hidden fields you can save this file under protected/components folder

this tutorial does not only apply to date filtering but also to other forms of filtering that is outside the CGridView

Date Column Class

class DateColumn extends CDataColumn{
    //put your code here
    public $from_date;
    public $to_date;
    public function renderFilterCellContent() {
        echo CHtml::activeDateField($this->grid->filter, $this->name, array('id'=>false));
            echo CHtml::activeHiddenField($this->grid->filter,$this->from_date);
            echo CHtml::activeHiddenField($this->grid->filter,$this->to_date);


Now we have to edit our CGridView file and make our date column to use this class file i am using the yii's default admin.php and _search.php

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'opportunity-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
            'name' => 'name',
            //here is where we use our DateColumn class
            //another example of customized column
            'name' => 'stage',
            'filter' => $stage_list,
            'value' => '$this->filter[$data->stage]',

The extended filter form (i placed this inside my _search.php)

$form = $this->beginWidget('CActiveForm', array(
    'method' => 'get',
//you can replace the DateField inputs with CJuiDatePicker
echo $form->dateField($model, 'start_date');
echo $form->dateField($model, 'end_date');
echo $form->submitButton();

if you don't have, you can add the following line of code to your admin.php this makes our form above submit via ajax and then reload the grid with the response data

Yii::app()->clientScript->registerScript('search', "
    $('#opportunity-grid').yiiGridView('update', {
        data: $(this).serialize()
    return false;


we just now have to add the two attributes

class Opportunity extends CActiveRecord{
    public $start_date;
    public $end_date;

then don't forget to add the validation rules

public function rules(){
     return array(....
      array('start_date, end_date','safe','on'=>'search')
public function search() {
if ($this->start_date != null) {
            $criteria->params += array('from' => date('Y-m-d', strtotime($this->start_date)));
if ($this->end_date != null) {
            $criteria->params += array('to' => date('Y-m-d', strtotime($this->end_date)));


Total 5 comments

#15133 report it
Shachish at 2013/10/10 04:29am
gives error ....

ok ,

thanks ezekielnoob

#15131 report it
ezekielnoob at 2013/10/10 03:55am

i don't think it is supported in 1.1.9.... you can just use textfields instead or consider upgrading to the latest version

#15130 report it
Shachish at 2013/10/10 03:30am
gives error ....

version - 1.1.9

in my application we use date picker but i want to add date range ...

#15124 report it
ezekielnoob at 2013/10/09 10:47am
RE: gives error ...

What version of yii are you using?

#15119 report it
Shachish at 2013/10/09 02:25am
gives error ...

Error 500 CActiveForm and its behaviors do not have a method or closure named "dateField".

How to fix this???

Leave a comment

Please to leave your comment.

Write new article