ExistValidator.php 2.81 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8
namespace yii\validators;
Qiang Xue committed
9 10

use Yii;
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
	 * 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;

Qiang Xue committed
40 41 42 43 44 45 46 47

	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
48
			$this->message = Yii::t('yii', '{attribute} is invalid.');
Qiang Xue committed
49 50 51
		}
	}

w  
Qiang Xue committed
52 53 54
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
Alexander Makarov committed
55
	 *
Qiang Xue committed
56
	 * @param \yii\db\ActiveRecord $object the object being validated
w  
Qiang Xue committed
57 58
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
59
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
60 61
	{
		$value = $object->$attribute;
Alexander Makarov committed
62

63
		if (is_array($value)) {
Qiang Xue committed
64
			$this->addError($object, $attribute, $this->message);
65 66 67
			return;
		}

Qiang Xue committed
68
		/** @var $className \yii\db\ActiveRecord */
Qiang Xue committed
69 70
		$className = $this->className === null ? get_class($object) : Yii::import($this->className);
		$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
Qiang Xue committed
71
		$query = $className::find();
Qiang Xue committed
72
		$query->where(array($attributeName => $value));
Qiang Xue committed
73
		if (!$query->exists()) {
Qiang Xue committed
74
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
75 76
		}
	}
Qiang Xue committed
77 78 79 80 81 82 83 84 85

	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 * @throws InvalidConfigException if either [[className]] or [[attributeName]] is not set.
	 */
	public function validateValue($value)
	{
86 87 88
		if (is_array($value)) {
			return false;
		}
Qiang Xue committed
89 90 91 92 93 94 95 96 97 98 99 100
		if ($this->className === null) {
			throw new InvalidConfigException('The "className" property must be set.');
		}
		if ($this->attributeName === null) {
			throw new InvalidConfigException('The "attributeName" property must be set.');
		}
		/** @var $className \yii\db\ActiveRecord */
		$className = $this->className;
		$query = $className::find();
		$query->where(array($this->attributeName => $value));
		return $query->exists();
	}
w  
Qiang Xue committed
101 102
}