From e0ace833d0e12b22339b42da3433ceddc7c9054f Mon Sep 17 00:00:00 2001 From: PowerGamer1 Date: Mon, 11 Apr 2016 11:59:34 +0300 Subject: [PATCH] Added Error message for combined attributes to unique validator - error message `$comboNotUnique` to include model attribute labels instead of attribute names. - Fixes #11322 close #11323 --- framework/CHANGELOG.md | 1 + framework/validators/UniqueValidator.php | 39 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 9993695c48..6df177e126 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/validators/UniqueValidator.php b/framework/validators/UniqueValidator.php index b0f415e6ad..5e677ddf0f 100644 --- a/framework/validators/UniqueValidator.php +++ b/framework/validators/UniqueValidator.php @@ -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; /** @@ -69,6 +78,9 @@ class UniqueValidator extends Validator 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.'); + } } /** @@ -133,7 +145,32 @@ class UniqueValidator extends Validator } 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)]); + } }