ExistValidator.php 2.47 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
Alexander Makarov committed
3
 * ExistValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10
namespace yii\validators;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
12

w  
Qiang Xue committed
13
/**
Alexander Makarov committed
14
 * ExistValidator validates that the attribute value exists in a table.
w  
Qiang Xue committed
15 16 17 18 19
 *
 * This validator is often used to verify that a foreign key contains a value
 * that can be found in the foreign table.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Alexander Makarov committed
20
 * @since 2.0
w  
Qiang Xue committed
21
 */
Alexander Makarov committed
22
class ExistValidator extends Validator
w  
Qiang Xue committed
23 24
{
	/**
Qiang Xue committed
25
	 * @var string the ActiveRecord class name or alias of the class
Alexander Makarov committed
26
	 * that should be used to look for the attribute value being validated.
Qiang Xue committed
27
	 * Defaults to null, meaning using the ActiveRecord class of
Alexander Makarov committed
28
	 * the attribute being validated.
w  
Qiang Xue committed
29 30 31 32
	 * @see attributeName
	 */
	public $className;
	/**
Qiang Xue committed
33
	 * @var string the yii\db\ActiveRecord class attribute name that should be
w  
Qiang Xue committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47
	 * used to look for the attribute value being validated. Defaults to null,
	 * meaning using the name of the attribute being validated.
	 * @see className
	 */
	public $attributeName;
	/**
	 * @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.
Alexander Makarov committed
48
	 *
Qiang Xue committed
49
	 * @param \yii\db\ActiveRecord $object the object being validated
w  
Qiang Xue committed
50
	 * @param string $attribute the attribute being validated
Qiang Xue committed
51
	 * @throws InvalidConfigException if table doesn't have column specified
w  
Qiang Xue committed
52
	 */
w  
Qiang Xue committed
53
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
54 55
	{
		$value = $object->$attribute;
Alexander Makarov committed
56
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
57
			return;
Alexander Makarov committed
58 59
		}

Qiang Xue committed
60
		/** @var $className \yii\db\ActiveRecord */
Alexander Makarov committed
61 62
		$className = ($this->className === null) ? get_class($object) : \Yii::import($this->className);
		$attributeName = ($this->attributeName === null) ? $attribute : $this->attributeName;
Qiang Xue committed
63
		$table = $className::getTableSchema();
Alexander Makarov committed
64
		if (($column = $table->getColumn($attributeName)) === null) {
Qiang Xue committed
65
			throw new InvalidConfigException('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"');
Alexander Makarov committed
66
		}
w  
Qiang Xue committed
67

Qiang Xue committed
68 69 70
		$query = $className::find();
		$query->where(array($column->name => $value));
		if (!$query->exists()) {
71
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} "{value}" is invalid.');
Qiang Xue committed
72
			$this->addError($object, $attribute, $message);
w  
Qiang Xue committed
73 74 75 76
		}
	}
}