Yii Framework Forum: Problem with cgridview filter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with cgridview filter Rate Topic: -----

#1 User is offline   ralphwigum 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 17-November 14

Posted 01 June 2017 - 09:36 AM

Hi there!

I have a big question. Maybe someone can help me.

I have a CGridview with userid column. That column is defined as an integer:
public function rules(){                
    return array(
        array('id_user','numerical','integerOnly'=>true),
        array('id_user', 'safe','on'=>'search'),
    );
}


When I write "abc" in the gridview filter, the controller does:

$model->attributes=$_POST['User'];


Next, load the view with the CGridview. That gridview gets data from the dataprovider. Returned from model:

$model->search();


As
$model->id_user
contains an invalid character no numerical, the SQL fails (Oracle).

I can use
$model->validation()
, the problem is where. In controller, in model...

Any idea?

Thanks.
0

#2 User is offline   dragan.zivkovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 200
  • Joined: 13-September 12

Posted 21 June 2017 - 02:30 PM

I don't think problem is anywhere, I mean $model->search will work with assigned attribute values. What ever is set as filter on page will be added in query. What you can do is :
i search method :
if (is_numeric($this->user_id)
   $criteria->addCondition('t.user_id = ' . $this->user_id);

meaning only if user_id is numeric it will apply condition. If it's not numeric user_id will not be used in query. That should solve sql issue you have
1

#3 User is offline   benelli 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 29-May 14

Posted 17 July 2017 - 05:51 AM

View Postdragan.zivkovic, on 21 June 2017 - 02:30 PM, said:

I don't think problem is anywhere, I mean $model->search will work with assigned attribute values. What ever is set as filter on page will be added in query. What you can do is :
i search method :
if (is_numeric($this->user_id)
   $criteria->addCondition('t.user_id = ' . $this->user_id);

meaning only if user_id is numeric it will apply condition. If it's not numeric user_id will not be used in query. That should solve sql issue you have



That was the solution we took for the same issue, but i think it should be also controlled in the view with any kind of js validation.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users