1: <?php
2: /**
3: * CExistValidator class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CExistValidator validates that the attribute value exists in a table.
13: *
14: * This validator is often used to verify that a foreign key contains a value
15: * that can be found in the foreign table.
16: *
17: * When using the {@link message} property to define a custom error message, the message
18: * may contain additional placeholders that will be replaced with the actual content. In addition
19: * to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
20: * CExistValidator allows for the following placeholders to be specified:
21: * <ul>
22: * <li>{value}: replaced with value of the attribute.</li>
23: * </ul>
24: *
25: * @author Qiang Xue <qiang.xue@gmail.com>
26: * @package system.validators
27: */
28: class CExistValidator extends CValidator
29: {
30: /**
31: * @var boolean whether the comparison is case sensitive. Defaults to true.
32: * Note, by setting it to false, you are assuming the attribute type is string.
33: */
34: public $caseSensitive=true;
35: /**
36: * @var string the ActiveRecord class name that should be used to
37: * look for the attribute value being validated. Defaults to null,
38: * meaning using the ActiveRecord class of the attribute being validated.
39: * You may use path alias to reference a class name here.
40: * @see attributeName
41: */
42: public $className;
43: /**
44: * @var string the ActiveRecord class attribute name that should be
45: * used to look for the attribute value being validated. Defaults to null,
46: * meaning using the name of the attribute being validated.
47: * @see className
48: */
49: public $attributeName;
50: /**
51: * @var mixed additional query criteria. Either an array or CDbCriteria.
52: * This will be combined with the condition that checks if the attribute
53: * value exists in the corresponding table column.
54: * This array will be used to instantiate a {@link CDbCriteria} object.
55: */
56: public $criteria=array();
57: /**
58: * @var boolean whether the attribute value can be null or empty. Defaults to true,
59: * meaning that if the attribute is empty, it is considered valid.
60: */
61: public $allowEmpty=true;
62:
63: /**
64: * Validates the attribute of the object.
65: * If there is any error, the error message is added to the object.
66: * @param CModel $object the object being validated
67: * @param string $attribute the attribute being validated
68: * @throws CException if given table does not have specified column name
69: */
70: protected function validateAttribute($object,$attribute)
71: {
72: $value=$object->$attribute;
73: if($this->allowEmpty && $this->isEmpty($value))
74: return;
75:
76: if(is_array($value))
77: {
78: // https://github.com/yiisoft/yii/issues/1955
79: $this->addError($object,$attribute,Yii::t('yii','{attribute} is invalid.'));
80: return;
81: }
82:
83: $className=$this->className===null?get_class($object):Yii::import($this->className);
84: $attributeName=$this->attributeName===null?$attribute:$this->attributeName;
85: $finder=$this->getModel($className);
86: $table=$finder->getTableSchema();
87: if(($column=$table->getColumn($attributeName))===null)
88: throw new CException(Yii::t('yii','Table "{table}" does not have a column named "{column}".',
89: array('{column}'=>$attributeName,'{table}'=>$table->name)));
90:
91: $columnName=$column->rawName;
92: $criteria=new CDbCriteria();
93: if($this->criteria!==array())
94: $criteria->mergeWith($this->criteria);
95: $tableAlias = empty($criteria->alias) ? $finder->getTableAlias(true) : $criteria->alias;
96: $valueParamName = CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount++;
97: $criteria->addCondition($this->caseSensitive ? "{$tableAlias}.{$columnName}={$valueParamName}" : "LOWER({$tableAlias}.{$columnName})=LOWER({$valueParamName})");
98: $criteria->params[$valueParamName] = $value;
99:
100: if(!$finder->exists($criteria))
101: {
102: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} "{value}" is invalid.');
103: $this->addError($object,$attribute,$message,array('{value}'=>CHtml::encode($value)));
104: }
105: }
106:
107: /**
108: * Given active record class name returns new model instance.
109: *
110: * @param string $className active record class name.
111: * @return CActiveRecord active record model instance.
112: *
113: * @since 1.1.14
114: */
115: protected function getModel($className)
116: {
117: return CActiveRecord::model($className);
118: }
119: }
120: