'Field value must be unique', ); public function __construct(Qs_Db_Table $table, $column, $current = null, array $options = array()) { $this->_table = $table; $this->_column = $column; $this->_current = $current; if (is_array($options) && !empty($options)) { $this->setOptions($options); } } public function setOptions($options) { if (isset($options['options'])) { unset($options['options']); } foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (method_exists($this, $method)) { // Setter exists; use it $this->$method($value); } } return $this; } public function setWhere($where) { if (is_array($where)) { $wherePart = array(); $db = $this->_table->getAdapter(); foreach ($where as $field => $value) { if (ctype_digit($field) || $field === 0) { $wherePart[] = $value; } else { $wherePart[] = $db->quoteInto($field . ' = ?', $value); } } $where = $wherePart; } $this->_where = (array) $where; return $this; } public function getWhere() { return $this->_where; } public function getJoin() { return $this->_join; } public function setJoin($join) { $this->_join = (array) $join; return $this; } public function isValid($value, $context = null) { $value = (string) $value; $this->_setValue($value); $db = $this->_table->getAdapter(); $tableAlias = Qs_Db::getTableAlias($this->_table->getName()); $select = $db->select(); $select->from(Qs_Db::getPair($tableAlias), ['found' => new Zend_Db_Expr('1')]); $select->limit(1); $where = $this->getWhere(); $where[] = $db->quoteInto($this->_column . ' = ?', $value); if (isset($this->_current)) { $current = (array) $this->_current; $info = $this->_table->info(); foreach ($info['primary'] as $key => $column) { $keyVal = Qs_Array::get($current, $key - 1); if ($keyVal) { $where[] = $db->quoteInto($column . ' <> ?', $keyVal); } } } foreach ($where as $cond) { $select->where($cond); } foreach ((array) $this->getJoin() as $args) { $select->join($args[0], $args[1], []); } if ($db->fetchOne($select)) { $this->_error(self::NOT_UNIQUE); return false; } return true; } }