hd.
hd.

Reputation: 18346

filter and CActiveDataProvider in CGridView

I have these code in index action of controller:

public function actionIndex()
{ 
  $cid = @$_GET['cid'];
  $country = Country::model()->findByPk($cid);

  if($cid)
    $dataProvider=new CActiveDataProvider('City', array(
      'criteria'=>array(
        'condition'=>'ci_co_id ='.$cid,
      ),
    ));
  else
     $dataProvider=new CActiveDataProvider('City'); 
  $this->render('index',array(
 'dataProvider'=>$dataProvider,
     'country' => $country
    ));
  }

and these in view/index.php file:

<?php 
$this->widget('zii.widgets.grid.CGridView', array(
  'id'=>'city-grid',
  'dataProvider'=>$dataProvider,
  'filter' => $dataProvider,
  'columns'=>array(
    array(
        'name' => ' ',
        'value' => '$row + 1',
    ),  
    'ci_name',
    'ci_pcode',
    array(
        'class'=>'CButtonColumn',
    ),
   )
));

?>

but Yii gives me this error:

CActiveDataProvider and its behaviors do not have a method or closure named "getValidators". 

What is the problem?

Upvotes: 2

Views: 4623

Answers (1)

Willem Renzema
Willem Renzema

Reputation: 5187

The filter has to be a class that extends CModel. However, you don't seem to be doing any actual filtering, so you can just comment the filter line of your CGridView out.

As a side note, you have major security hole in your criteria. You are leaving yourself wide open to an SQL injection attack.

Specify your criteria as follows to let the database handler properly escape the input:

'criteria'=>array(
  'condition'=>'ci_co_id =:cid',
  'params'=>array(':cid'=>$cid),
),

Upvotes: 2

Related Questions