/** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ protected function validateAttribute($object, $attribute) { $with = explode(",", $this->with); if (count($with) < 1) { throw new Exception("Attribute 'with' not set"); } $uniqueValidator = new CUniqueValidator(); $uniqueValidator->attributes = array($attribute); $uniqueValidator->message = $this->message; $uniqueValidator->on = $this->on; $conditionParams = array(); $params = array(); foreach ($with as $attribute) { $conditionParams[] = "`{$attribute}`=:{$attribute}"; $params[":{$attribute}"] = $object->{$attribute}; } $condition = implode(" AND ", $conditionParams); $uniqueValidator->criteria = array('condition' => $condition, 'params' => $params); $uniqueValidator->validate($object); }
/** * @param \CModel $object * @param string $attribute * * @throws Exception * @return null */ protected function validateAttribute($object, $attribute) { $with = explode(',', $this->with); if (count($with) < 1) { throw new Exception(Craft::t('Attribute “with” not set.')); } $uniqueValidator = new \CUniqueValidator(); $uniqueValidator->attributes = array($attribute); $uniqueValidator->message = $this->message; $uniqueValidator->on = $this->on; $conditionParams = array(); $params = array(); foreach ($with as $column) { $conditionParams[] = "`{$column}`=:{$column}"; $params[":{$column}"] = $object->{$column}; } $condition = implode(' AND ', $conditionParams); $uniqueValidator->criteria = array('condition' => $condition, 'params' => $params); $uniqueValidator->validate($object); }
protected function validateAttribute($object, $attribute) { if ($object->id == 1) { $this->allowEmpty = true; } $langs = array_keys(I18nActiveRecord::getLangs()); $attr = $attribute; $p = explode('_', $attribute); if (in_array($p[0], $langs)) { $attr = $p[1]; } foreach ($langs as $lang) { $this->attributeName = $lang . '_' . $attr; $value = $object->{$this->attributeName}; $p = explode('/', $value); if (!$this->allowEmpty && ($value == '' || empty($p[count($p) - 1])) || in_array($value, $this->restrictedUrls())) { $this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.')); } parent::validateAttribute($object, $attribute); } }
/** * {@inheritDoc} * @see CUniqueValidator::validateAttribute() */ protected function validateAttribute($object, $attribute) { if (empty($this->attributeList) || count($this->attributeList) === 0) { return parent::validateAttribute($object, $attribute); } $value = $object->{$attribute}; if ($this->allowEmpty && $this->isEmpty($value)) { return; } if (is_array($value)) { // https://github.com/yiisoft/yii/issues/1955 $this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.')); return; } $className = $this->className === null ? get_class($object) : Yii::import($this->className); $finder = $this->getModel($className); $table = $finder->getTableSchema(); $columns = array(); foreach ($this->attributeList as $attributeName) { if (($column = $table->getColumn($attributeName)) === null) { throw new CException(Yii::t('yii', 'Table "{table}" does not have a column named "{column}".', array('{column' => $attributeName, '{table}' => $table->name))); } $columns[] = $column; } $criteria = new CDbCriteria(); if ($this->criteria !== array()) { $criteria->mergeWith($this->criteria); } $tableAlias = empty($criteria->alias) ? $finder->getTableAlias(true) : $criteria->alias; foreach ($columns as $column) { /* @var $column CDbColumnSchema */ $columnName = $column->rawName; $valueParamName = CDbCriteria::PARAM_PREFIX . CDbCriteria::$paramCount++; $criteria->addCondition($this->caseSensitive ? "{$tableAlias}.{$columnName}={$valueParamName}" : "LOWER({$tableAlias}.{$columnName})=LOWER({$valueParamName})"); $criteria->params[$valueParamName] = $value; } if (!$object instanceof CActiveRecord || $object->isNewRecord || $object->tableName() !== $finder->tableName()) { $exists = $finder->exists($criteria); } else { $criteria->limit = 2; $objects = $finder->findAll($criteria); $n = count($objects); if ($n === 1) { $exists = false; foreach ($columns as $column) { if ($column->isPrimaryKey) { // primary key is modified and not unique $exists |= $object->getOldPrimaryKey() != $object->getPrimaryKey(); } else { // non primary key, need to exclude the current record based on pk $exists |= array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey(); } } } else { $exists = $n > 1; } } if ($exists) { $values = array(); foreach ($this->attributeList as $attribute1) { $values[] = $this->{$attribute1}; } $message = $this->message !== null ? $this->message : Yii::t('yii', '{attributeList} "{valueList}" has already been taken.'); $this->addError($object, $attribute, $message, array('{attributeList}' => CHtml::encode(implode(', ', $this->attributeList)), '{valueList}' => CHtml::encode(implode(', ', $values)))); } }