yii在管理页面的搜索中添加验证

时间:2012-08-03 14:54:54

标签: php yii

我有coba2的控制器。 on function actionAdmin我将在搜索上添加验证,因为当整数被非整数搜索时,它会给出SQL错误

CDbCommand failed to execute the SQL statement: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "a" LINE 1: SELECT COUNT(*) FROM "yii_user" "t" WHERE id_user='a' ^. The SQL statement executed was: SELECT COUNT(*) FROM "yii_user" "t" WHERE id_user=:ycp0

这是我在我的模型上调用验证功能的函数

public function actionAdmin()
{
    $model=new coba2('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['coba2']))
                $model->attributes=$_GET['coba2'];

            if($model->validate()){
                $this->render('admin',array(
                        'model'=>$model,
                ));
            } else{
                $model->unsetAttributes();
                $this->render('admin',array(
                        'model'=>$model,
                ));
            }
}

这是我模型中的规则

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('username, password, salt, date_create, date_update, date_birth', 'required'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
                    array('id_user', 'numerical', 'integerOnly'=>true, 'on'=>'search'),
        array('id_user, username, password, salt, date_create, date_update, date_birth', 'safe', 'on'=>'search'),
    );
}
抱歉,我的英语不好,但我需要帮助。 谢谢:))

1 个答案:

答案 0 :(得分:1)

你有id_user作为'integerOnly'=>true, 'on'=>'search',尝试将以下规则添加到你的模型中,这会强制id_user在所有场景中都是数字,而不仅仅是'搜索':

array('id_user', 'numerical', 'integerOnly'=>true),