From a0e614eb20a045864fce5187ab513cd2066ebf6d Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Sun, 20 Mar 2016 16:34:42 +0200 Subject: [PATCH] `yii\validators\EachValidator` injects specific attribute value in error message parameters Closes #11139 --- framework/CHANGELOG.md | 3 ++- framework/validators/EachValidator.php | 7 ++++++- framework/validators/Validator.php | 6 ++++-- tests/framework/validators/EachValidatorTest.php | 24 +++++++++++++++++++++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 813c76e..88d7113 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -26,7 +26,8 @@ Yii Framework 2 Change Log - Enh #10610: Added `BaseUrl::$urlManager` to be able to set URL manager used for creating URLs (samdark) - Enh #10764: `yii\helpers\Html::tag()` and `::beginTag()` return content without any HTML when the `$tag` attribute is `false` or `null` (pana1990) - Enh #10941: Added `yii\helpers\ArrayHelper::isTraversable`, added support for traversable selections for dropdownList, radioList and checkboxList in `yii\helpers\Html` (sammousa) -- Eng #10976: `Inflector::transliterate()` now uses `strtr` instead of `str_replace` (DrDeath72) +- Enh #10976: `Inflector::transliterate()` now uses `strtr` instead of `str_replace` (DrDeath72) +- Enh #11139: `yii\validators\EachValidator` injects specific attribute value in error message parameters (silverfire) - Enh: Added `StringHelper::countWords()` that given a string returns number of words in it (samdark) - Bug #11040: Check parameter 'recursive' and disable recursive copying with option 'recursive' => false in method BaseFileHelper::copyDirectory (Ni-san) - Chg: HTMLPurifier dependency updated to `~4.6` (samdark) diff --git a/framework/validators/EachValidator.php b/framework/validators/EachValidator.php index 51fc281..0899898 100644 --- a/framework/validators/EachValidator.php +++ b/framework/validators/EachValidator.php @@ -146,7 +146,12 @@ class EachValidator extends Validator } $result = $validator->validateValue($v); if ($result !== null) { - return $this->allowMessageFromRule ? $result : [$this->message, []]; + if ($this->allowMessageFromRule) { + $result[1]['value'] = $v; + return $result; + } else { + return [$this->message, ['value' => $v]]; + } } } diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index 4199e68..e3a37d4 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -356,9 +356,11 @@ class Validator extends Component */ public function addError($model, $attribute, $message, $params = []) { - $value = $model->$attribute; $params['attribute'] = $model->getAttributeLabel($attribute); - $params['value'] = is_array($value) ? 'array()' : $value; + if (!isset($params['value'])) { + $value = $model->$attribute; + $params['value'] = is_array($value) ? 'array()' : $value; + } $model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language)); } diff --git a/tests/framework/validators/EachValidatorTest.php b/tests/framework/validators/EachValidatorTest.php index be94e4f..98e6b86 100644 --- a/tests/framework/validators/EachValidatorTest.php +++ b/tests/framework/validators/EachValidatorTest.php @@ -74,6 +74,28 @@ class EachValidatorTest extends TestCase } /** + * @depends testValidate + */ + public function testCustomMessageValue() + { + $model = FakedValidationModel::createWithAttributes([ + 'attr_one' => [ + 'TEXT', + ], + ]); + $validator = new EachValidator(['rule' => ['integer', 'message' => '{value} is not an integer']]); + + $validator->validateAttribute($model, 'attr_one'); + $this->assertSame('TEXT is not an integer', $model->getFirstError('attr_one')); + + $model->clearErrors(); + $validator->allowMessageFromRule = false; + $validator->message = '{value} is invalid'; + $validator->validateAttribute($model, 'attr_one'); + $this->assertEquals('TEXT is invalid', $model->getFirstError('attr_one')); + } + + /** * @see https://github.com/yiisoft/yii2/issues/10825 * * @depends testValidate @@ -86,4 +108,4 @@ class EachValidatorTest extends TestCase $validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => false]]); $this->assertFalse($validator->validate([''])); } -} \ No newline at end of file +}