mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
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
This commit is contained in:
committed by
Carsten Brandt
parent
393d945a7b
commit
e0ace833d0
@ -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)
|
||||
|
||||
|
||||
|
||||
@ -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)]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user