1.在AR的rules中的定义
public function rules() { return array( array('name', 'unique', 'criteria' => array('condition' => "`user_site_id`=>'{$this->user_site_id}'")), ); }
2.自定义验证规则
public function rules() { return array( array('name', 'validateSiteUnique'); ); } public function validateSiteUnique($attribute, $param) { if (!$this->hasErrors()) { $siteUniqueCriteria = new CDbCriteria(); $siteUniqueCriteria->addCondition("`user_site_id`='{$this->user_site_id}'"); $validator = new CUniqueValidator(); $validator->attributes = array($attribute); $validator->criteria = $siteUniqueCriteria; $validator->validate($this); } }