UniqueValidator.php 2.55 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;
12
use yii\db\ActiveRecord;
Carsten Brandt committed
13
use yii\db\ActiveRecordInterface;
w  
Qiang Xue committed
14

w  
Qiang Xue committed
15
/**
Alexander Makarov committed
16
 * UniqueValidator validates that the attribute value is unique in the corresponding database table.
w  
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Alexander Makarov committed
19
 * @since 2.0
w  
Qiang Xue committed
20
 */
Alexander Makarov committed
21
class UniqueValidator extends Validator
w  
Qiang Xue committed
22 23
{
	/**
Qiang Xue committed
24
	 * @var string the ActiveRecord class name or alias of the class
Alexander Makarov committed
25
	 * that should be used to look for the attribute value being validated.
Qiang Xue committed
26
	 * Defaults to null, meaning using the ActiveRecord class of the attribute being validated.
w  
Qiang Xue committed
27 28 29 30 31 32 33 34 35 36
	 * @see attributeName
	 */
	public $className;
	/**
	 * @var string the ActiveRecord class attribute name that should be
	 * used to look for the attribute value being validated. Defaults to null,
	 * meaning using the name of the attribute being validated.
	 */
	public $attributeName;

Qiang Xue committed
37
	/**
Qiang Xue committed
38
	 * @inheritdoc
Qiang Xue committed
39 40 41 42 43
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
44
			$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
Qiang Xue committed
45 46 47
		}
	}

w  
Qiang Xue committed
48
	/**
Qiang Xue committed
49
	 * @inheritdoc
w  
Qiang Xue committed
50
	 */
w  
Qiang Xue committed
51
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
52 53
	{
		$value = $object->$attribute;
54 55

		if (is_array($value)) {
56
			$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
57 58 59
			return;
		}

slavcodev committed
60
		/** @var \yii\db\ActiveRecord $className */
Qiang Xue committed
61
		$className = $this->className === null ? get_class($object) : $this->className;
Qiang Xue committed
62
		$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
w  
Qiang Xue committed
63

Qiang Xue committed
64
		$query = $className::find();
65
		$query->where([$attributeName => $value]);
Alexander Makarov committed
66

Carsten Brandt committed
67
		if (!$object instanceof ActiveRecordInterface || $object->getIsNewRecord()) {
Qiang Xue committed
68
			// if current $object isn't in the database yet then it's OK just to call exists()
Qiang Xue committed
69
			$exists = $query->exists();
Alexander Makarov committed
70 71
		} else {
			// if current $object is in the database already we can't use exists()
Qiang Xue committed
72 73
			$query->limit(2);
			$objects = $query->all();
w  
Qiang Xue committed
74 75

			$n = count($objects);
Alexander Makarov committed
76
			if ($n === 1) {
77
				if (in_array($attributeName, $className::primaryKey())) {
Alexander Makarov committed
78
					// primary key is modified and not unique
w  
Qiang Xue committed
79
					$exists = $object->getOldPrimaryKey() != $object->getPrimaryKey();
Alexander Makarov committed
80
				} else {
w  
Qiang Xue committed
81 82 83
					// non-primary key, need to exclude the current record based on PK
					$exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey();
				}
Alexander Makarov committed
84
			} else {
w  
Qiang Xue committed
85
				$exists = $n > 1;
Alexander Makarov committed
86
			}
w  
Qiang Xue committed
87 88
		}

Alexander Makarov committed
89
		if ($exists) {
Qiang Xue committed
90
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
91 92
		}
	}
Zander Baldwin committed
93
}