ExistValidator.php 2.4 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

	/**
Qiang Xue committed
42
	 * @inheritdoc
Qiang Xue committed
43 44 45 46 47
	 */
	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
	/**
Qiang Xue committed
53
	 * @inheritdoc
w  
Qiang Xue committed
54
	 */
w  
Qiang Xue committed
55
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
56 57
	{
		$value = $object->$attribute;
Alexander Makarov committed
58

59
		if (is_array($value)) {
Qiang Xue committed
60
			$this->addError($object, $attribute, $this->message);
61 62 63
			return;
		}

slavcodev committed
64
		/** @var \yii\db\ActiveRecord $className */
Qiang Xue committed
65
		$className = $this->className === null ? get_class($object) : $this->className;
Qiang Xue committed
66
		$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
Qiang Xue committed
67
		$query = $className::find();
Alexander Makarov committed
68
		$query->where([$attributeName => $value]);
Qiang Xue committed
69
		if (!$query->exists()) {
Qiang Xue committed
70
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
71 72
		}
	}
Qiang Xue committed
73 74

	/**
Qiang Xue committed
75
	 * @inheritdoc
Qiang Xue committed
76
	 */
Qiang Xue committed
77
	protected function validateValue($value)
Qiang Xue committed
78
	{
79
		if (is_array($value)) {
Qiang Xue committed
80
			return [$this->message, []];
81
		}
Qiang Xue committed
82 83 84 85 86 87
		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.');
		}
slavcodev committed
88
		/** @var \yii\db\ActiveRecord $className */
Qiang Xue committed
89 90
		$className = $this->className;
		$query = $className::find();
Alexander Makarov committed
91
		$query->where([$this->attributeName => $value]);
Qiang Xue committed
92
		return $query->exists() ? null : [$this->message, []];
Qiang Xue committed
93
	}
w  
Qiang Xue committed
94
}