date range search

Hello, im trying to create a "between date search" with the inbuilt datepicker.

I have followed this guide http://www.yiiframework.com/forum/index.php/topic/20941-filter-date-range-on-cgridview-toolbar/

but I wan’t to include the datesearch in the advanced searchfield instead, tried just pasting in the datepicker code in the _search view but it doesn’t do the search.

Is there something I should change to make it work?

Hi. Maybe you should post what you’ve done that doesn’t work?

Im sorry.

I am trying to create a daterange searchfield and this is what i have come up with:

In my model:




public $date_first;

public $date_last;


public function rules()

	{

		return array(

			array('..., date_first, date_last', 'safe', 'on'=>'search'),

		);

	}


public function search()

	{

		$criteria=new CDbCriteria;

		

		$criteria->compare( ... );

		

		if((isset($this->date_first) && trim($this->date_first) != "") && (isset($this->date_last) && trim($this->date_last) != ""))

                        $criteria->addBetweenCondition('from_date', ''.$this->date_first.'', ''.$this->date_last.'');

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



And this is in the _search view(datepicker):




$this->widget('zii.widgets.jui.CJuiDatePicker', array(

                                        // 'model'=>$model,

                                    'name' => 'Event[date_first]',

                                    'language' => 'id',

                                        'value' => $model->date_first,

                                    // additional javascript options for the date picker plugin

                                    'options'=>array(

                                        'showAnim'=>'fold',

                                        'dateFormat'=>'yy-mm-dd',

                                        'changeMonth' => 'true',

                                        'changeYear'=>'true',

                                        'constrainInput' => 'false',

                                    ),

                                    'htmlOptions'=>array(

                                        'style'=>'height:20px;width:70px;',

                                    ),

                                ),true) ;


$this->widget('zii.widgets.jui.CJuiDatePicker', array(

                                        // 'model'=>$model,

                                    'name' => 'Event[date_last]',

                                    'language' => 'id',

                                        'value' => $model->date_last,

                                    // additional javascript options for the date picker plugin

                                    'options'=>array(

                                        'showAnim'=>'fold',

                                        'dateFormat'=>'yy-mm-dd',

                                        'changeMonth' => 'true',

                                        'changeYear'=>'true',

                                        'constrainInput' => 'false',

                                    ),

                                    'htmlOptions'=>array(

                                        'style'=>'height:20px;width:70px',

                                    ),

                                ),true);



This does not provide an error but the search functionality doesn’t work.

Any ideas?

I cannot see what would be really wrong with your code, although:

[list=1]

[*]Well your db fields are of [font="Courier New"]date[/font] type, right, and not [font="Courier New"]datetime[/font]?

[*]After many tries, I ended up with a different syntax for CJuiDatePicker, which would look like this:


<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'model'=>$model,

    'attribute'=>'date_first',

    'name'=>'Event[date_first]',

    // I don't think it fits here

    //'value' => $model->date_first, 

    …

));

?>

…

<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'model'=>$model,

    'attribute'=>'date_last',

    'name'=>'Event[date_last]',

    // same as above

    //'value' => $model->date_last

    …

));

?>

[*]Third thing you can check, the SQL output, what does it look like?

[/list]

PS Until now, I used only classical addCondition construction for date and datetime fields. So my remarks apply at least for that case.

I tried your new CJuiDatePicker syntax and it works now!

Thank you very much! :lol: