Skip to content

Commit

Permalink
Added Error message for combined attributes to unique validator
Browse files Browse the repository at this point in the history
- error message `$comboNotUnique` to include model attribute labels instead of attribute names.
- Fixes yiisoft#11322

close yiisoft#11323
  • Loading branch information
PowerGamer1 authored and cebe committed Jul 6, 2016
1 parent 393d945 commit e0ace83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Yii Framework 2 Change Log
- Enh #11212: Added headers to PO file in `yii\i18n\GettextPoFile::save()` (stevekr)
- Bug #6347: `inverseOf()` not working for dynamic relational queries (laszlovl)
- Bug #10613: Fixed PostgreSQL Schema to return correct column names for unique indexes that have mixed case column names (cebe)
- Bug #11322: Fixed incorrect error message in `yii\validators\UniqueValidator` for composite `targetAttribute` (PowerGamer1, silverfire, cebe)
- Chg #11364: Updated jQuery dependency to include versions `1.12.*` (cebe)


Expand Down
39 changes: 38 additions & 1 deletion framework/validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class UniqueValidator extends Validator
* is the [[\yii\db\Query|Query]] object that you can modify in the function.
*/
public $filter;
/**
* @var string the user-defined error message used when [[targetAttribute]] is an array. It may contain the following placeholders:
*
* - `{attributes}`: the labels of the attributes being validated.
* - `{values}`: the values of the attributes being validated.
*
* @since 2.0.9
*/
public $comboNotUnique;


/**
Expand All @@ -69,6 +78,9 @@ public function init()
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
}
if ($this->comboNotUnique === null) {
$this->comboNotUnique = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.');
}
}

/**
Expand Down Expand Up @@ -133,7 +145,32 @@ public function validateAttribute($model, $attribute)
}

if ($exists) {
$this->addError($model, $attribute, $this->message);
if (is_array($targetAttribute)) {
$this->addComboNotUniqueError($model, $attribute);
} else {
$this->addError($model, $attribute, $this->message);
}
}
}

/**
* Builds and adds [[comboNotUnique]] error message to the specified model attribute.
* @param \yii\base\Model $model the data model.
* @param string $attribute the name of the attribute.
*/
private function addComboNotUniqueError($model, $attribute)
{
$attributeCombo = [];
$valueCombo = [];
foreach ($this->targetAttribute as $key => $value) {
if(is_int($key)) {
$attributeCombo[] = $model->getAttributeLabel($value);
$valueCombo[] = '"' . $model->$value . '"';
} else {
$attributeCombo[] = $model->getAttributeLabel($key);
$valueCombo[] = '"' . $model->$key . '"';
}
}
$this->addError($model, $attribute, $this->comboNotUnique, ['attributes' => implode(', ', $attributeCombo), 'values' => implode(', ', $valueCombo)]);
}
}

0 comments on commit e0ace83

Please sign in to comment.