Browse Source

`yii\validators\EachValidator` injects specific attribute value in error message parameters

Closes #11139
tags/2.0.8
SilverFire - Dmitry Naumenko 9 years ago
parent
commit
a0e614eb20
  1. 3
      framework/CHANGELOG.md
  2. 7
      framework/validators/EachValidator.php
  3. 6
      framework/validators/Validator.php
  4. 24
      tests/framework/validators/EachValidatorTest.php

3
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)

7
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]];
}
}
}

6
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));
}

24
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(['']));
}
}
}

Loading…
Cancel
Save