Validation Rules Only For Updating Database


what is the best way of using validation rules: Just for things in the db or also for getting records?

For example: You want to get a specific entry from your database and check the given parameter.

Would you create a rule for it:


array('val', 'required'),

array('val', 'length', 'min'=>3),

array('val', 'exists')


$model->val = $_GET['val'];

if($model->validate()) {

  //do search


or do that without a validator by checking the variable in the controller:

if(isset($_GET['val']) && length($_GET['val'])>3...) {

   //do search


//EDIT: You know, upda.ting is a spam word, because it contains da.ting :D

Hi trilipio.

According to Yii and MVC architectural the best way is using validators in Model

Even you want more complex validator you could create one by yourself

So you whoud do something like this?

$model = new Mymodel("searchtitle");

$model->title = $_GET['title'];

if($model->validate()) {

  $model = Mymodel->find("title=?", array($model->title));


No, you wouldn’t. Why do you want to validate when you select records? You only need validation when inserting or updating.

Okay, so you use validation rules only for data and if you want to validate other User input e.g. for a select, you use normal php methods?

But Take a look at the auto-generated code of a model. Dont they use validation rules for the search method, which is clearly Not a upda.te/insert operation?

In search case, It is no nessesary to validate the user inputs because Yii Framework with code like that

  $criteria = new CDbCriteria;

  $criteria->compare('id', $this->id);

manipulates secured compares.