This is probably better written as a method-based validator or a customized validator class. The reason is that the query condition could vary a lot in different applications. Some need static parameters while some dynamic parameters. Writing a method-based validator would be much easier.
I was able to solve this issue using a method based validator (and not a bulk of code) according to your suggestion. And I would like to share this with others as a solution.
public function rules()
{
return array(
(snip)
array('user', 'uniqueInForum'),
);
}
/**
* Unique validator in a forum
*/
public function uniqueInForum() {
$criteria=new CDbCriteria;
$criteria->condition ='`forumcode` ='."'".$this->forumcode."'";
$criteria->condition .=' AND `user` ='."'".$this->user."'";
$userCount=user::model()->count($criteria->condition);
if ($userCount)
$this->addError('user', 'User "'.$this->user.'" has already been taken.');
}