Browse Source

Fix BC break for `UniqueValidator` custom message when validating multiple attributes

tags/2.0.10
Robert Korulczyk 8 years ago
parent
commit
2f981c5a70
  1. 1
      framework/CHANGELOG.md
  2. 33
      framework/validators/UniqueValidator.php

1
framework/CHANGELOG.md

@ -35,6 +35,7 @@ Yii Framework 2 Change Log
- Bug #11973: Fixed `yii\helpers\BaseHtml::getAttributeValue()` to work with `items[]` notation correctly (silverfire) - Bug #11973: Fixed `yii\helpers\BaseHtml::getAttributeValue()` to work with `items[]` notation correctly (silverfire)
- Bug #12100: Fixed `yii\filters\HttpCache` was sending an empty Pragma header (sergeymakinen) - Bug #12100: Fixed `yii\filters\HttpCache` was sending an empty Pragma header (sergeymakinen)
- Bug #12107: Fixed REST Serializer to validate input for 'expand' and 'fields' parameter, which crashed on array input (njspok, cebe) - Bug #12107: Fixed REST Serializer to validate input for 'expand' and 'fields' parameter, which crashed on array input (njspok, cebe)
- Bug #12152: Fixed BC break for `UniqueValidator` custom message when validating multiple attributes (rob006)
- Enh #12230: Allows BaseHtml::activeListInput to override the field value (RangelReale) - Enh #12230: Allows BaseHtml::activeListInput to override the field value (RangelReale)

33
framework/validators/UniqueValidator.php

@ -60,12 +60,24 @@ class UniqueValidator extends Validator
*/ */
public $filter; public $filter;
/** /**
* @var string the user-defined error message used when [[targetAttribute]] is an array. It may contain the following placeholders: * @var string the user-defined error message. When validating single attribute, it may contain
* the following placeholders which will be replaced accordingly by the validator:
*
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
*
* When validating mutliple attributes, it may contain the following placeholders:
* *
* - `{attributes}`: the labels of the attributes being validated. * - `{attributes}`: the labels of the attributes being validated.
* - `{values}`: the values of the attributes being validated. * - `{values}`: the values of the attributes being validated.
* *
*/
public $message;
/**
* @var string
* @since 2.0.9 * @since 2.0.9
* @deprecated Deprecated since version 2.0.10, to be removed in 2.1. Use [[message]] property
* to setup custom message for multiple target attributes.
*/ */
public $comboNotUnique; public $comboNotUnique;
@ -76,11 +88,18 @@ class UniqueValidator extends Validator
public function init() public function init()
{ {
parent::init(); parent::init();
if ($this->message === null) { if ($this->message !== null) {
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.'); return;
} }
if ($this->comboNotUnique === null) { if (is_array($this->targetAttribute) && count($this->targetAttribute) > 1) {
$this->comboNotUnique = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.'); // fallback for deprecated `comboNotUnique` property - use it as message if is set
if ($this->comboNotUnique === null) {
$this->message = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.');
} else {
$this->message = $this->comboNotUnique;
}
} else {
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
} }
} }
@ -153,7 +172,7 @@ class UniqueValidator extends Validator
} }
} }
} }
/** /**
* Builds and adds [[comboNotUnique]] error message to the specified model attribute. * Builds and adds [[comboNotUnique]] error message to the specified model attribute.
* @param \yii\base\Model $model the data model. * @param \yii\base\Model $model the data model.
@ -172,7 +191,7 @@ class UniqueValidator extends Validator
$valueCombo[] = '"' . $model->$key . '"'; $valueCombo[] = '"' . $model->$key . '"';
} }
} }
$this->addError($model, $attribute, $this->comboNotUnique, [ $this->addError($model, $attribute, $this->message, [
'attributes' => Inflector::sentence($attributeCombo), 'attributes' => Inflector::sentence($attributeCombo),
'values' => implode('-', $valueCombo) 'values' => implode('-', $valueCombo)
]); ]);

Loading…
Cancel
Save