* @link http://www.yiiframework.com/ * @copyright Copyright © 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ /** * CUniqueValidator validates that the attribute value is unique in the corresponding database table. * * CUniqueValidator can only be used for active record objects. * * @author Qiang Xue * @version $Id$ * @package system.validators * @since 1.0 */ class CUniqueValidator extends CValidator { /** * @var boolean whether the comparison is case sensitive. Defaults to true. * Note, by setting it to false, you are assuming the attribute type is string. */ public $caseSensitive=true; /** * @var boolean whether the attribute value can be null or empty. Defaults to true, * meaning that if the attribute is empty, it is considered valid. */ public $allowEmpty=true; /** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param CModel the object being validated * @param string the attribute being validated */ protected function validateAttribute($object,$attribute) { $value=$object->$attribute; if($this->allowEmpty && ($value===null || $value==='')) return; $columnName=$object->getTableSchema()->getColumn($attribute)->rawName; if($this->caseSensitive) $exists=$object->exists("$columnName=:value",array(':value'=>$value)); else $exists=$object->exists("LOWER($columnName)=LOWER(:value)",array(':value'=>$value)); if($exists) { $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} "{value}" has already been taken.'); $this->addError($object,$attribute,$message,array('{value}'=>$value)); } } }